Alex90 Posted May 27, 2012 Share Posted May 27, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/263227-php-and-executing-sql-file-to-the-database/ Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263227-php-and-executing-sql-file-to-the-database/#findComment-1349037 Share on other sites More sharing options...
Barand Posted May 27, 2012 Share Posted May 27, 2012 php doesn't let you perform multiple queries. Not quite true - it's the mysql lib that doesn't allow them. OTOH mysqli lib does allow them. Quote Link to comment https://forums.phpfreaks.com/topic/263227-php-and-executing-sql-file-to-the-database/#findComment-1349040 Share on other sites More sharing options...
Alex90 Posted May 27, 2012 Author Share Posted May 27, 2012 Understood... In the matter of fact, you should be able, as PHP.net documentation says... http://php.net/manual/en/mysqli.multi-query.php The same function I am using in my script... So I thought it gets the file into the string incorrectly, as the only error I am getting is that there is something wrong in the comment. Thanks anyway! Quote Link to comment https://forums.phpfreaks.com/topic/263227-php-and-executing-sql-file-to-the-database/#findComment-1349041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.