0

我有这张桌子玩游戏

scores (user,score)

里面有一些条目,我有这个查询:

SET @row_num = 0; 
SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC

它适用于 phpmyadmin 但不适用于 php 代码

$query = "set @row_num = 0; SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC";
$result = mysql_query($query) or die(mysql_error());

php已正确连接到mysql数据库,在此之前我做了其他查询,一切正常

它说:

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 'SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY s' at line 1

你认为它可能是什么?谢谢你,亚历山德罗

4

3 回答 3

6

这是因为 phpMyAdmin 会为您拆分分号查询。MySQL 服务器不这样做,PHPmysql扩展也不这样做。

您需要将它们分成两个单独的调用mysql_query

旁注:mysql不推荐使用 PHP 扩展,建议您将PDOmysqli用于任何新代码。请在您未来的开发过程中牢记这一点。

于 2013-01-08T14:19:39.513 回答
2

首先mysql_query是贬义。

您不能在一次调用中运行两个查询mysql_query

$query = "set @row_num = 0; SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC";

您必须执行以下操作:

$query1= "set @row_num = 0;";
$query2 = "SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
于 2013-01-08T14:19:58.360 回答
1

尝试拆分为 2 个查询:

$query = "set @row_num = 0;";
$query2 = " SELECT @row_num := @row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC;";
$result = mysql_query($query) or die(mysql_error());
$result1 = mysql_query($query2);
于 2013-01-08T14:20:32.697 回答