douliao8402 2014-11-07 21:06
浏览 36
已采纳

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

when I run the search script I'm getting the following error Trying to get property of non-object in C:\xampp\htdocsesults.php on line 32 Why is it telling me it is not an object? It obviously gives a similar error on line 47. I would appreciate any help. Thank you

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?>
  • 写回答

1条回答 默认 最新

  • douzhuo1858 2014-11-07 21:10
    关注

    Because your query failed. You need to check your return value, and inspect the error message.

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

    ABC: Always Be Checking [your return values]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程