Php 试图获取$result上非对象错误属性的数据库搜索结果

Php 试图获取$result上非对象错误属性的数据库搜索结果,php,mysql,Php,Mysql,当我运行搜索脚本时,我得到以下错误 试图在第32行的C:\xampp\htdocs\results.php中获取非对象的属性 为什么它告诉我它不是一个物体?很明显,它在第47行给出了类似的错误。 我将感谢任何帮助。 多谢各位 7<?php 8 // create short variable names 9 $searchtype=$_POST['searchtype']; 10 $searchterm=trim($_POST['searchterm']); 11 12 if (!$

当我运行搜索脚本时,我得到以下错误 试图在第32行的C:\xampp\htdocs\results.php中获取非对象的属性 为什么它告诉我它不是一个物体?很明显,它在第47行给出了类似的错误。 我将感谢任何帮助。 多谢各位

7<?php
8  // create short variable names
9  $searchtype=$_POST['searchtype'];
10  $searchterm=trim($_POST['searchterm']);
11
12 if (!$searchtype || !$searchterm) {
13    echo 'You have not entered search details.  Please go back and try again.';
14     exit;
15 }
16
17  if (!get_magic_quotes_gpc()){
18    $searchtype = addslashes($searchtype);
19    $searchterm = addslashes($searchterm);
20  }
21
22  @ $db = new mysqli('localhost', 'printfactory0', '*********', 'printfactory');
23
24  if (mysqli_connect_errno()) {
25     echo 'Error: Could not connect to database.  Please try again later.';
26     exit;
27  }
28
29  $query = "select * from printfactory where ".$searchtype." like '%".$searchterm."%'";
30  $result = mysqli_query($db, $query);
31
32  $num_results = $result->num_rows;
33
34  echo "<p>Number of products found: ".$num_results."</p>";
35
36  for ($i=0; $i <$num_results; $i++) {
37     $row = $result->fetch_assoc();
38     echo "<p><strong>".($i+1).". Product Name: ";
39     echo htmlspecialchars(stripslashes($row['ProductName']));
40     echo "</strong><br />Product Description: ";
41     echo stripslashes($row['Product_Description']);
42     echo "<br />Price: ";
43     echo stripslashes($row['Unit_Price']);
44     echo "</p>";
45  }
46
47  $result->free();
48  $db->close();
49
50?>

7因为您的查询失败。您需要检查返回值,并检查错误消息

if( ! $result = mysqli_query($db, $query) ) {
    die(mysqli_error($db));
}

ABCA始终BeC检查[返回值]

您没有进行任何错误检查,或者您知道您的查询有问题。谢谢。我会尽快检查的。抱歉,我对codingok很陌生,我有时间检查错误,结果在第29行中,我输入了数据库的名称,而不是必须进行搜索的表的名称。谢谢@Sammitch。非常有用。我试图投票支持这条评论,但由于我缺乏声誉,它不让我这么做。