php/mysql 函数的奇怪行为
Posted
技术标签:
【中文标题】php/mysql 函数的奇怪行为【英文标题】:Strange behavior of a php/mysql function 【发布时间】:2014-12-30 02:01:56 【问题描述】:我正在做一个项目,我必须在其中显示玩家的排行榜。但是当编写下面的代码时,它显示了 2, 4, 6, 8, ... 的排名,而不是奇数排名。谁能告诉我“这有什么问题?”
$query1_string = "CREATE VIEW Leaderboard AS SELECT Name, Points, PhoneNo
FROM user ORDER BY Points DESC";
$query2_string = "set @rank = 0";
$query3_string = "SELECT @rank := @rank + 1 as Rank, Name, Points
FROM Leaderboard";
$query5_string = "DROP VIEW Leaderboard";
// Doing the queries
$query1 = mysqli_query($con, $query1_string) or die(mysqli_error($con));
$query2 = mysqli_query($con, $query2_string) or die(mysqli_error($con));
$query3 = mysqli_query($con, $query3_string) or die(mysqli_error($con));
// Initializing the count
$count = 0;
//Making an array of strings including Rank, Name and Points of the Top 5 Players
while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM)))
$row = mysqli_fetch_array($query3, MYSQL_NUM);
$results[$count] = $row[0] . " " . $row[1] . " " . $row[2];
$count++;
// Dropping the view.
$end_query = mysqli_query($con, $query5_string) or die(mysqli_error($con));
//Returning the array
$leader = implode("\n", $results);
echo $leader;
【问题讨论】:
它显示的是偶数,你想显示奇数吗? 我想展示两者。 @яша 【参考方案1】:您正在执行 query3 两次并显示从第二次执行中检索到的数据
while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM)))
$row = mysqli_fetch_array($query3, MYSQL_NUM);
【讨论】:
是的!就是这样。我被蒙蔽了:)谢谢以上是关于php/mysql 函数的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章