Jump to content

PHP and executing .sql file to the Database


Alex90

Recommended Posts

Hello there fellow programmers!

 

I am writing to find some sort of solution to my php script.

 

The problem is, that while I try to run function that will execute multiple queries to the database (I have also tried to separate them and run as separated queries), I just simply get the error.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '--

--

-- Database: testdb

--

--' at line 4

 

And no matter how I'd change the file (I delete comments, I make different type of comments or what so ever), I'll get the same sort of error in first line or some where similar...

 

Now the File I am loading up, is 100% stable, as I have import it using phpMyAdmin as also just simply paste the file content into the SQL at the phpMyAdmin. It all worked fine.

 

My PHP code is:

 

$sqlfile = file_get_contents('backup.sql');
if (!mysqli_multi_query($connect, $sqlfile)) {
$err[] = mysqli_error($connect);
}

 

I have also tried closing it in those functions:

 

trim
utf8_encode
nl2br

 

It did not change anything...

 

And just to avoid some explanations, the $connect is correct, I did use the whole connection few times within the script and I have executed different types of queries before.

 

Many thanks for your advice and help!

Link to comment
Share on other sites

php doesn't let you perform multiple queries.  I think the least amount of work you can do will involve using exec or system and run a mysql import via command line.  But if that's not an option, you're going to have to parse .sql to grab each sql query individually..which sounds like you tried to do but didn't parse it correctly. It may be easier for you to separate exports, one for structure and one for data, and export the data as csv.  That might make it easier for you to read the data line by line and insert, using fgetcsv

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.