从sql文件加载时,mysql_query无法正常工作

时间:2013-03-27 19:01:21

标签: php mysql phpmyadmin

我已将数据库的内容以类似

的形式转储到sql文件中
insert into `a` values
(17,11,5),
(18,12,7),
(19,12,10),
(21,14,45),
(22,15,46),
(24,16,46),
(25,16,49),
(26,17,21),
(27,17,30),
(28,17,45),
(29,17,54),
(30,18,32),
(31,18,35),
(32,19,23),
(33,19,27),
(34,19,54),
(35,20,53),
(36,21,32),
(37,21,35),
(38,21,45),
(39,22,23),
(40,22,30),
(41,22,45),
(57,24,19),
(58,25,46),
(59,26,39),
(60,27,49),
(61,27,56),
(62,28,34);

insert into `b` values (14,'2009-01-06',''),
(15,'2009-02-01',''),
(16,'2009-03-01',''),
(17,'2009-03-25',''),
(18,'2009-04-05',''),
(19,'2009-04-17',''),
(20,'2009-04-18',''),
(21,'2009-04-19',''),
(22,'2009-04-23',''),
(24,'2009-07-05',''),
(25,'2009-08-02',''),
(26,'2009-08-07',''),
(27,'2009-09-06',''),
(28,'2009-09-14','');

etc..

我有4个这样的表,没有外围键约束。然后我尝试将数据上传到db(mysql)。我读了文件的内容,我将每个表的插入传递给一个数组然后我为每个元素做了mysql_query:

$sqlArray = explode(';',$sqlFile); 

 for($i=0;$i<sizeof($sqlArray);$i++){
   mysql_query($sqlArray[$i]) or die ('Error: '.mysql_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 ' insert into `a` values (17,11,5), (18,12,7), (19,12,10), (21,14,4' at line 1

我验证了$ sqlArray具有正确的包含,并且查询是正确的,并且可以从phpmyadmin运行。

问题似乎与第一个表无关(即,即使b是第一个,它也会出现)并且似乎总是出现#34; cut&#34;中间的查询(或差不多70个字符后)。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

你的第二个陈述有拼写错误(“int”应该是“into”):

insert int `b` values (14,'2009-01-06',''),

或者,如果这不是问题,请尝试使用单独的insert语句来获得更清晰的错误消息:

insert into `a` values (17,11,5);
insert into `a` values (118,12,7);
...

我在一些旧版本的MySQL上遇到过他们不喜欢扩展插入的经历。您还可以尝试使用列的实际名称明确指定列名称。如果这些表中有超过3列(例如自动增量列),这将非常有用。

insert into `a` (`[column1name]`, `[column2name]`, `[column3name]`) values (17,11,5);