I want to create a utility in PHP like phpMyAdmin’s import option, which should allow database updates to the remote server via a .sql file without creating a new database.
Since it’s a client side utility, access to cpanel is not allowed.
The app has two kinds of working environments, offline & online.
If the client works offline, they need to take the backup of database and should update the database with remote server similar for online.
Then they have to update the database of remote server.
Solution 1
If you are running your PHP on a Linux system, you can try using the ‘mysql’ command itself. However please note that your PHP installation has the permission to run “system” commands, like system(), exec() etc.
So here is what I mean to say:
Please replace,
{db_user_name} with the DB username,
{db_host} with the DB host,
{db_password} with the DB password,
{full_path_to_your_sql_file} with the path to your SQL file.
And this of course requires the SQL file to be uploaded.
Solution 2:
Read the SQL file line by line and while reading execute each statement using PHP’s standard MySQL library. Something like:
However, this might not be as simple as it seems. If your SQL file has comments and other .sql specific statements, you might need to put checks to ignore them. Or better if the SQL file contains nothing but SQL statements.