使用连接在php中执行sql查询

时间:2012-08-29 16:33:30

标签: php sql

  

可能重复:
  Mysql can't perform more than 1 query at a time

        $query = "TRUNCATE TABLE nw_world;";
        $query = $query . " INSERT INTO `nw_world`";
        $query = $query . " SELECT * FROM `x_world` WHERE x <0 AND y >=0";
        $query = $query . " AND tid !=5 AND aid NOT IN ( 29, 908, 935, 941, 950 )";
        $query = $query . " AND population <=50";

        echo "$query";
        mysql_query($query,$con) or die("error ".mysql_error());

这会导致错误

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 `nw_world` SELECT * FROM `x_world` WHERE x <0 AND y >=0 AND tid !=5 ' at line 1

但是当我在mysql中执行相同的查询时,它工作正常。由于这个我有一种感觉,我在PHP编码中犯了一些错误。请帮忙

2 个答案:

答案 0 :(得分:3)

mysql_query不允许您执行多个查询。使用mysqli::multi_query 或单独执行每个命令。

答案 1 :(得分:3)

默认情况下,每个mysql_query调用(安全措施)只会执行1个查询。

如果您想一次执行多个,请查看http://se2.php.net/manual/en/mysqli.multi-query.php