gpt4 book ai didi

php - 尝试在 PHP 中使用 "SET @rownum = 0;"时出错

转载 作者:行者123 更新时间:2023-11-30 01:18:38 24 4
gpt4 key购买 nike

当我在 mysql 中测试这个查询时,它很好,但是当我在 php 中运行它时,我不断收到此错误。

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'SELECT *, (@rownum := @rownum + 1) ASrank FROM ( SELECT *, (totalWins+(total' at line 1) 附近使用的正确语法

这是我的 php 代码。

    <?php

$sql = " SET @rownum = 0; ";

$sql .= " SELECT *, (@rownum := @rownum + 1) AS rank FROM ( ";

$sql .= " SELECT *, (totalWins+(totalPushs*.5)) AS totalPoints, totalWins+totalLost+totalPushs AS totalBets FROM ( ";

$sql .= " SELECT *, SUM(win) AS totalWins, SUM(lost) AS totalLost, SUM(push) AS totalPushs FROM ( ";

$sql .= " SELECT *, (finalResult = 'Winner') AS win, (finalResult = 'Loser') AS lost, (finalResult = 'Push') AS push FROM ( ";

$sql .= " SELECT " . $db_prefix . "users.userID, userName, ";
$sql .= " IF (pickID=visitorID, visitorResult, homeResult) AS finalResult ";
$sql .= " FROM " . $db_prefix . "users ";
$sql .= " JOIN " . $db_prefix . "picks ";
$sql .= " ON " . $db_prefix . "users.userID = " . $db_prefix . "picks.userID ";
$sql .= " JOIN " . $db_prefix . "schedule ";
$sql .= " ON " . $db_prefix . "picks.gameID = " . $db_prefix . "schedule.gameID ";

$sql .= " ) x ";

$sql .= " ) x ";
$sql .= " GROUP BY userID ";

$sql .= " ) x ";

$sql .= " ) x ";
$sql .= " ORDER BY totalPoints DESC, totalWins DESC, totalPushs DESC, totalLost ";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

echo $row[rank] . '|' . $row[userName]. '|' . $row[totalWins] . '|' . $row[totalLost] . '|' . $row[totalPushs] . '|' . $row[totalPoints];
echo '<br>';

}

?>

无需第一行代码,我就可以让 php 代码正常工作

$sql = "SET @rownum = 0; ";

但它不会回显排名列。

当其中一段代码在 php 中时,我是否必须采取不同的措施来行该代码?

最佳答案

mysql_query 不支持一次运行多个查询。您必须先运行
mysql_query("SET @rownum = 0;");然后您可以在第二个 mysql_query 调用中运行查询的其余部分。

关于php - 尝试在 PHP 中使用 "SET @rownum = 0;"时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com