如何获得一个在PHPmyadmin中工作的mysql脚本,以便在PHP mysql查询中工作?
问题描述:
Simple question with i'm sure a simple answer. I have searched around and can't find a solution. Basically my code below works in Phpmyadmin but it wont work in a PHP mysql query script. The code orders my "rank" row by the largest number, and then updates the "position" row with a number, starting at 1 for the greatest number in the "rank" column, and then incrementing the "position" row as the "rank" row values become smaller.
SET @r=0;
UPDATE Data SET position= @r:= (@r+1) ORDER BY rank DESC;
SET @r=0;
SELECT *, @r:= (@r+1) as position FROM Data ORDER BY rank DESC;
Thanks in advance!!
简单的问题,我确定一个简单的答案。 我搜索过,无法找到解决方案。 基本上我的代码在Phpmyadmin中工作,但它不适用于PHP mysql查询脚本。 代码将我的“rank”行按最大数字排序,然后用数字更新“position”行,从“rank”列中的最大数字开始,然后将“position”行增加为“ 排名“行值变小。 p>
SET @ r = 0;
UPDATE数据集位置= @r:=(@ r + 1)ORDER BY rank DESC;
SET @ r = 0;
SELECT *,@ r:=(@ r + 1)作为位置FROM数据ORDER BY rank DESC;
code> pre>
提前致谢!! p>
div>
答
You have multiple queries here. Try using mysqli_multi_query:
$result = mysqli_multi_query($con,"SET @r=0;
UPDATE Data SET position= @r:= (@r+1) ORDER BY rank DESC;
SET @r=0; SELECT *, @r:= (@r+1) as position
FROM Data ORDER BY rank DESC;") ;