2013-09-29 33 views
1

我在php中使用原始(不变)ezsql-mysql类。我的数据库引擎是innodb。一切都很好,但在一个页面中只有一个查询。当我从一些其他工作页面复制代码库时,所有需要的类和功能都已添加。就在问题查询之前还有另一个正常工作的查询。当我直接将它复制粘贴到phpmyadmin时,带有问题的查询也在php文件之外工作。Mysql查询不能在Php文件中工作

$db->query(" 
    BEGIN; 
    INSERT INTO places (name, latitude, longitude) 
     VALUES ('Place of $event_name', '$latitude', '$longitude'); 
    INSERT INTO events (place_code, event_name, start_time, owner_id) 
     VALUES (LAST_INSERT_ID(),'$event_name', '$start_time', '$owner_id'); 
    COMMIT; 
    "); 
//this echo part was just for copying to phpmyadmin 
    echo " 
    BEGIN; 
    INSERT INTO places (name, latitude, longitude) 
     VALUES('Place of $event_name', '$latitude', '$longitude'); 
    INSERT INTO events (place_code, event_name, start_time, owner_id) 
     VALUES(LAST_INSERT_ID(),'$event_name', '$start_time', '$owner_id'); 
    COMMIT; 
    "; 

每当我使用该页时,我的服务器会在该页的目录中生成错误日志。且误差

[29-Sep-2013 12:58:00] PHP Warning: 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 places (name, latitude, longitude) 
    VALUES ('Place of asd', '39.76' at line 2 in /home7/eyselnet/public_html/business/injoin/demo/sql/ez_sql_mysql.php on line 233 

,这就是发生在我只是复制和在phpMyAdmin贴上相同的查询

BEGIN;# MySQL returned an empty result set (i.e. zero rows). 
INSERT INTO places (name, latitude, longitude) VALUES('Place of asd', '39.7617657', '30.5056083');# 1 row affected. 
INSERT INTO events (place_code, event_name, start_time, owner_id) VALUES(LAST_INSERT_ID(),'asd', '1357884000', '1');# 1 row affected. 
COMMIT;# MySQL returned an empty result set (i.e. zero rows). 

能否请你帮我找到了什么问题?在最糟糕的情况下,我可以更改我的代码以使其以不同的方式工作。但是,我正在寻求帮助来理解。因为它看起来很奇怪,以至于在phpmyadmin上正常工作的查询在php文件中有问题。

在此先感谢。

+0

不要将'begin'和'commit'作为同一查询的一部分......如果您使用的是事务性数据库引擎,请将它们作为单独查询 –

回答

0

我不知道提到的类,但mysql(i)功能在PHP(我猜PDO,也)只让你执行一个查询每个函数调用。因此,您需要将您的BEGIN;COMMIT;分隔为自己的MySQL查询。

编辑:你当然必须分别做两个INSERTs

+0

是的,这是真的。可能这是问题所在。当我快速搜索时,我发现; [mysqli_multi_query](http://se2.php.net/manual/en/mysqli.multi-query.php) 但显然我的ezsql类不支持。 –

+0

请注意,每次调用一次查询的限制也是一种防止某些类型的SQL注入的安全机制。除非你真的需要,否则不要使用mysql [i] _multi_query()'(并且我不能真正想到这种情况)。 – TheWolf

+0

好的,谢谢。我只是认为使用更少的连接,更少的查询来优化事情会更好。 –