我做了这个查询,向我展示了排名'按点排序的结果。查询工作正常但由于某种原因,我没有得到任何结果,试图用PHP获取它 也许有人可以告诉我为什么?
(int) $a = $_GET['a'];
if($ranking = $con->prepare("SET @rownum := 0; SELECT rank, id FROM (SELECT @rownum := @rownum + 1 AS rank, id FROM anime ORDER BY points DESC) as result WHERE id=$a"))
{
$ranking->execute();
$ranking->next_result();
$ranking->bind_result($rank, $id);
while($ranking->fetch())
{
var_dump($rank, $id);
if($id == $a)
{
echo "<span class='rnknr'>#$rank</span>";
}
}
$ranking->close();
}
}
答案 0 :(得分:0)
$ ranking-&GT;执行();如果查询包含多个表达式,则可能返回多个结果集。
答案 1 :(得分:0)
试试这个
(int) $a = $_GET['a'];
if($ranking = $con->prepare("SET @rownum := 0; SELECT rank, id FROM (SELECT @rownum := @rownum + 1 AS rank, id FROM anime ORDER BY points DESC) as result WHERE id=1"))
{
$ranking->execute();
$ranking->next_result(); //<- advance to the result set for 2nd query (The select query)
$ranking->bind_result($rank, $id);
while($ranking->fetch())
{
if($id == $a)
{
echo "<span class='rnknr'>#$rank</span>";
}
}
$ranking->close();
}
您的语句中有2个查询,因此只有调用fetch才会显示第一个查询SET @rownum := 0
的结果。您需要通过调用next_result
答案 2 :(得分:0)
你必须使用mysqli::multi_query中的一些特殊方法,因为这是多查询:)现在你的代码看起来像这样:
$mysqli = mysqli_connect(HOST, USER, PASS, DB);
#(int) $a = $_GET['a']; -> What is this ?
$a = (int)$_GET['a']; //This is simplest casting
$sql = 'SET @rownum := 0; SELECT rank, id FROM (SELECT @rownum := @rownum + 1 AS rank, id FROM anime ORDER BY points DESC) as result WHERE id='.$a;
if ($mysqli->multi_query($sql)) {
$mysqli->next_result();
$result = $mysqli->store_result();
while ($row = $result->fetch_assoc()) {
if($row['id'] == $a)
{
echo "rank: #{$row['rank']}";
}
}
}