phpMyAdmin中导入的程序化等效项 [英] Programmatic Equivalent Of Import in phpMyAdmin
问题描述
我有一个SqlDump.sql文件,当我使用phpMyAdmin的Import功能应用它时,它工作得很好,但是我需要能够以编程方式完成此操作.作为菜鸟,我试图做这样的事情:
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);
但是我遇到各种各样的mysql错误.但是,当我使用phpMyAdmin导入该文件时,该文件也可以正常工作.如何使它以编程方式工作?
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?
错误:
Can't create table 'somedb.t_sr_u_alertcode' (errno: 150)
推荐答案
您遇到了双重麻烦.
该错误是外键约束错误.您正在尝试创建的某个表可能具有到另一个尚未创建的表的外键.
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
通过谷歌搜索
- http://www.google.is/search?hl=zh_CN& ; q =可以不能+创建+表格++(错误号:+150)
- http://www.google.is/search?hl=en&q=Can't+create+table++(errno:+150)
第二个问题是,您可能会在尝试导入大文件时遇到麻烦,因为我认为您无法执行以;分隔的多个sql命令.就像您可以在phpmyadmin中一样.我建议使用这样的脚本:
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:
我自己还没有尝试过,但是我没有理由相信它不起作用.
I haven't tried it myself, but I have no reason to believe it doesn't work.
这篇关于phpMyAdmin中导入的程序化等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!