I have a SqlDump.sql file that works just fine when I apply it using the Import feature of phpMyAdmin, however I need to be able to accomplish this programmatically. Being a noob, I tried to do something like this:
$SQL=file_get_contents('SqlDump.sql');
$DB=mysqli_conne开发者_C百科ct('localhost','root','');
mysqli_select_db($DB,'somedb');
if (mysqli_multi_query($DB, $SQL)) {
do {
if ($result = mysqli_store_result($DB)) {
mysqli_free_result($result);
}
} while (mysqli_next_result($DB));
}
$Err=mysqli_error($DB);
mysqli_close($DB);
But I get all kinds of mysql errors. Yet the same file works just fine when I import it using phpMyAdmin. How do I get this to work programmatically?
Error:
Can't create table 'somedb.t_sr_u_alertcode' (errno: 150)
You have double-trouble.
That error is a foreign key constraint error. Some table you're trying to create probably has a foreign key to another table that hasn't yet been created.
I found some instances of the error here:
- http://www.brainfault.com/2008/02/15/mysql-error-1005-hy000-cant-create-table-tablefrm-errno-150/
- http://forums.mysql.com/read.php?22,19755,19755
by googling this:
- http://www.google.is/search?hl=en&q=Can't+create+table++(errno:+150)
Second problem is that you'll probably get into trouble trying to import a large file because I don't think you can execute multiple sql commands seperated with an ; like you can in phpmyadmin. I'd suggest using a script like this one:
- http://www.t4vn.net/example/showcode/SQL-script-import-function.html
I haven't tried it myself, but I have no reason to believe it doesn't work.
You could always just look at the source code
精彩评论