mysqli_num_rows返回false而不是true [关闭]

I have difficulties getting rows from a table in a database because mysqli_num_rows always returns false. This is an extract of my code:

$query = "SELECT * FROM table1 WHERE user = '$user'";
if (mysqli_num_rows(mysqli_query($conn, $query)))
{
$row = mysqli_fetch_row($results);
echo $row[1]
}

I did not get the result of the echo $row[1]. I then substituted the if statement with another:

    if (!mysqli_num_rows(mysqli_query($conn, $query)))
    echo 'no rows!'

and got 'no rows!' on the browser.

I have successfully connected to the database and other queries work just fine. Please, I would like to understand why mysqli_num_rows will return false even though I have rows in the table which match the query.

It should be

$query = "SELECT * FROM table1 WHERE user = '$user'";
if (mysql_num_rows(mysql_query($query)) > 0){
$row = mysql_fetch_row($results);
  echo $row[1];
}else{
  echo 'user does not exist'; 
}
$query = "SELECT * FROM table1 WHERE user = '$user'";
if (mysql_num_rows(queryMysql($query)))
{
$row = mysql_fetch_row($results);
echo $row[1]
}

Try Doing it step by step:

$Query = mysql_query($query);
$Results = mysql_num_rows($Query);

if ($Results === 0){
 echo "No Results";
}else{
 $row = mysql_fetch_row($query);
 echo $row[1];
}

This is what it should look if there are rows:

$query = "SELECT * FROM table1 WHERE user = '$user'";
if (mysql_num_rows(mysql_query($query)) != 0) {
$row = mysql_fetch_row($results);
echo $row[1]
}

This is what it should look like if there are no rows:

if (mysql_num_rows(mysql_query($query)) == 0) {
    echo 'no rows!'
}

The best way to do it would be like this:

$query = "SELECT * FROM table1 WHERE user = '$user'";
if (mysql_num_rows(mysql_query($query)) != 0) {
$row = mysql_fetch_row($results);
echo $row[1]
}else{
if (mysql_num_rows(mysql_query($query)) == 0) {
    echo 'no rows!'
}}

If the queryMysql method is custom then it is likely that it doesn't return a resource (i.e. it may return an array instead). A sign of this would be mysql_num_rows returning a literal false If this is the case you can use count on the array.

If mysql_num_rows is returning 0 rather than false then this means the query is genuinely returning no rows.