0

I try to google it but, i really dont understand what is the problem with this query. Here is code

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!";
}

This query gives me nothing. Just this error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in something on line 8, and if i delete ORDER BY DESC there is some error on line 10? Sorry if it repeated, but i have no idea to solve this!! Thank you!

bobouch
  • 57
  • 5

2 Answers2

2

Your SQL statement has no ORDER column:

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

Change it with the correct column name:

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

Probably, mysqli_query is returning false instead of mysqli_result object.

segarci
  • 739
  • 1
  • 11
  • 19
1

To add segarci,

$row = mysqli_fetch_array($sql) 

should be

$row = mysqli_fetch_array($user_query)
Maarten van Middelaar
  • 1,691
  • 10
  • 15