mysql查询在phpmyadmin中运行时有效,但在php中运行时返回错误

时间:2011-01-19 14:20:59

标签: mysql

使用以下函数,我试图将数据更新到数据库。直接在phpmyadmin中运行时查询效果很好,但从php运行时会产生错误。

这是函数

function edit_row($table, $columns, $where){
        db_connect();
        $query = "BEGIN WORK; SET AUTOCOMMIT=0; UPDATE $table SET $columns WHERE $where;    COMMIT;";
        echo $query; //this is to control for typing errors when testing in phpmyadmin
        mysql_query($query) or die ("Entry could not be made, " . mysql_error());
        db_close();
}

运行此命令:

edit_row("hello","test = 'some other string'", "test = 'somestring'");

回声输出:

BEGIN WORK; SET AUTOCOMMIT=0; UPDATE hello SET test = 'some other string' WHERE test = 'some string'; COMMIT;

错误产生:

Entry could not be made, 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 'SET AUTOCOMMIT=0; UPDATE hello SET test = 'some other string' WHERE test = 'so' at line 2

它似乎切断了查询字符串的最后一位,但不确定这是否是die()方法的怪癖

1 个答案:

答案 0 :(得分:4)

您无法在一次调用mysql_query中执行多个查询 - 您需要将查询分解为四个单独的调用。

根据PHP mysql_query documentation

  

mysql_query()发送一个唯一的查询   (不支持多个查询)   到当前活动的数据库   与之关联的服务器   指定link_identifier。

这在phpMyAdmin中起作用的原因是因为phpMyAdmin在解构输入的语句后实际上在后台执行了四个单独的查询。