Programmatic Equivalent Of Import in phpMyAdmin

允我心安 提交于 2019-12-10 10:44:59

问题


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_connect('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)

回答1:


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.




回答2:


You could always just look at the source code



来源:https://stackoverflow.com/questions/2562792/programmatic-equivalent-of-import-in-phpmyadmin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!