Mysqli num行和mysqli获取数组问题

时间:2014-03-12 22:28:04

标签: php sql mysqli

我尝试谷歌但是,我真的不明白这个查询有什么问题。这是代码

include_once("includes/db_connection.php");
//Upit za prikaz pitanja!
$listaPitanja = "";
$sql = "SELECT id, username, question FROM useroptions ORDER BY DESC";
$user_query = mysqli_query($db_connection, $sql);
$pitanjaCount = mysqli_num_rows($user_query); //line 8
if ($pitanjaCount > 0) {
while ($row = mysqli_fetch_array($sql))  { //line 10
$id = $row['id'];
$question = $row['question'];
$username = $row['username'];
$listaPitanja .= '<div id="brojOdgovora">'.$id.'</div>
            <div id="tekstPitanja"><h3>'.$question.'</h3></div>
            <div id="userPitanja"><h6>'.$username.'</h6></div>';
    } 
} else {
$listaPitanja = "There is no inserted questions!";
}

这个查询什么都没给我。只是这个错误mysqli_num_rows()期望参数1是mysqli_result,在第8行给出的是布尔值,如果我删除ORDER BY DESC,第10行会有一些错误? 对不起,如果重复,但我不知道解决这个问题!谢谢!

2 个答案:

答案 0 :(得分:2)

您的SQL语句没有ORDER列:

$sql = "SELECT id, username, question FROM useroptions ORDER BY DESC";

使用正确的列名更改它:

$sql = "SELECT id, username, question FROM useroptions ORDER BY column_name DESC";

可能mysqli_query正在返回false而不是mysqli_result个对象。

答案 1 :(得分:1)

添加segarci,

$row = mysqli_fetch_array($sql) 

应该是

$row = mysqli_fetch_array($user_query)