PHP& MySQL:mysqli_num_rows()错误.怎么了? [英] PHP & MySQL: mysqli_num_rows() error. What is wrong?

查看:99
本文介绍了PHP& MySQL:mysqli_num_rows()错误.怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了此代码以一次搜索多个单词,但是我的代码无法正常工作.我自己纠正的过程中出现了大多数错误,但是这种方法不起作用.

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:

警告:mysqli_num_rows()期望参数1为mysqli_result,在第26行的C:\ XAMPP \ htdocs \ test \ search.php中给出的布尔值

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

这是我的代码:

<?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.

推荐答案

我所做的只是添加了测试,以确保我们的$result不为空.这样,如果它为空,它将打印您的SQL语句以带到PhpMyAdmin或您选择的另一个DB Interaction Tool,以便您可以从那里调整查询.

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();

?>

这篇关于PHP&amp; MySQL:mysqli_num_rows()错误.怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆