PHP & MySQL: mysqli_num_rows() error. What is wron

2019-08-27 09:22发布

问题:

This question already has an answer here:

  • mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 32 answers
  • How to get MySQLi error information in different environments? / mysqli_fetch_assoc() expects parameter 1 to be mysqli_result 1 answer
  • Reference - What does this error mean in PHP? 34 answers

I created this code for searching multiple words at once, but my code doesn't work. Most of errors that appeared along the way I corrected by myself, but with this does not work.

When I hit the search button, it gives me this error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\XAMPP\htdocs\test\search.php on line 26

Here is my code:

<?php

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'dictionary';

$connect = new mysqli ($host, $user, $password, $database);

if (isset($_GET['search'])) {
  $condition = '';
  $query = explode(" ", $_GET['search']);
  foreach ($query as $text)
  {
    $condition .= "hieroglyphs LIKE '%".mysqli_real_escape_string($connect, $text)."%' OR";
  }
  $condition = substr($condition, 0, -4);
  $sql_query = "SELECT hieroglyphs, meaning FROM test_dictionary WHERE " . $condition;
  $result = mysqli_query($connect, $sql_query);
  if (mysqli_num_rows($result) > 0)
  {
     echo "<table><tr><th>Hieroglyphs</th><th>Meaning</th></tr>";
     while($row = mysqli_fetch_array($result))
     {
         echo "<tr><td>".$row["hieroglyphs"]."</td><td>".$row["meaning"]."</td></tr>";
     }
     echo "</table>";
  } else {
     echo "0 results";
  }
}

$connect->close();

?>

How to correct this error? Thank you in advance.

回答1:

All I did was add in there testing to make sure our $result wasn't empty. By doing this, if it is empty, it will print your SQL statement to bring over to PhpMyAdmin, or another DB Interaction Tool of your choice so you can adjust your query from there.

<?php

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'dictionary';

$connect = new mysqli ($host, $user, $password, $database);

if (isset($_GET['search'])) {
    $condition = '';
    $query = explode(" ", $_GET['search']);
    foreach ($query as $text) {
        $condition .= "hieroglyphs LIKE '%" . mysqli_real_escape_string($connect, $text) . "%' OR";
    }
    $condition = substr($condition, 0, -4);
    $sql_query = "SELECT hieroglyphs, meaning FROM test_dictionary WHERE " . $condition;
    $result = mysqli_query($connect, $sql_query);
    // if our result isn't empty, let's process
    if (!empty($result)) {
        if (mysqli_num_rows($result) > 0) {
            echo "<table><tr><th>Hieroglyphs</th><th>Meaning</th></tr>";
            while ($row = mysqli_fetch_array($result)) {
                echo "<tr><td>" . $row["hieroglyphs"] . "</td><td>" . $row["meaning"] . "</td></tr>";
            }
            echo "</table>";
        } else {
            echo "0 results";
        }
    } else {
        // If it is empty, we need to try and find out why.
        print "No results: " . $sql_query;
    }
}

$connect->close();

?>