@variable的PHP MYSQL更新查询不起作用

时间:2019-05-07 20:25:46

标签: php mysql variables

在php中,我尝试将表列调用order更新为增量值,步长为10,其中project =到1;

我的表名是:task 我的表包含列:id,项目,名称,顺序

在phpmyadmin中,我成功执行了该查询。

SET @order := 0; UPDATE `task` SET `order` = @order := @order + 10 WHERE project = 1 ;

现在在PHP中,我正在这样做:

$query = 'SET @order := 0; UPDATE `task` SET `order` = @order := @order + 10 WHERE project = "'.$project.'";';
$result = mysql_query($query) OR die(mysql_error());

如果我回显我的$ query,我有这个。

SET @order := 0; UPDATE `task` SET `order` = @order := @order + 10 WHERE project = "1"

我收到此错误:

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 'UPDATE `task` SET `order` = @order := @order + 10 WHERE project = "1" at line 1

知道我的问题是什么

我知道我应该使用PDO或更现代的SQL东西,但这是旧项目的补丁;)

1 个答案:

答案 0 :(得分:1)

您不能一次调用public class GridRowObject { public int total { get; private set; } public int page { get; private set; } public int records { get; private set; } public IEnumerable rows { get; private set; } public object userdata { get; set; } public GridRowObject(int total, int page, int records, IEnumerable rows) { this.total = total; this.page = page; this.records = records; this.rows = rows; } public GridRowObject(int total, int page, int records, IEnumerable rows, object userdata) : this(total, page, records, rows) { this.userdata = userdata; } } 来输入多个查询。将其分为两个调用:

mysql_query()

$query = 'SET @order := 0'; mysql_query($query) OR die(mysql_error()); $query = 'UPDATE `task` SET `order` = @order := @order + 10 WHERE project = "'.$project.'";'; mysql_query($query) OR die(mysql_error()); 之类的变量在两次调用之间仍然存在,因为它们与连接而不是与调用相关联。