0

我的 php mysql 查询不起作用,或者至少这是我的假设。如果你们可能会注意到我没有看到的东西,将不胜感激。谢谢!

以下是我收到的错误:

Notice: Trying to get property of non-object in D:\
Fatal error: Call to a member function free() on a non-object in D:\

我有 9 个带有 varchar 的列:

Part Number,Alternate Partnumber,Qty,
Description,Part Condition Code,Price,
Location,Barcode,Consignment

我创建了一个搜索栏,我希望客户能够输入零件编号,它会找到该零件编号及其行中的所有信息。

我正在使用Luke Welling 和 Laura Thomson 的Php 和 Mysql Web 开发,所以我使用的部分代码来自一章。

这是代码:

<?php
    //create short variable names
    $searchtype = "Part Number";
    $searchterm = $_POST['searchterm'];

    //echo "$searchtype";
    //echo "$searchterm";

    if(!$searchtype || !$searchterm)
    {
        echo 'You must enter a part number please try again';
        exit;
    }

    if(!get_magic_quotes_gpc())
    {
        $searchtype = addslashes($searchtype);
        $searchterm = addslashes($searchterm);
    }

    @ $db = new mysqli('localhost', 'username', 'password', 'partstest');

    if(mysqli_connect_errno())
    {
        echo 'Error: Could not connect to database. Please try again later.';
        exit;
    }

    //$query = "SELECT $searchterm * FROM inventory WHERE Part Number";
    $query = "select * from inventory where " .$searchtype. " like '%".$searchterm."%'";


    $result = $db->query($query);
    //$result = mysqli_query($db, $query);

    $num_results = $result->num_rows;
    //$num_results = mysqli_num_rows($result);

    echo "<p>Number of books found: ".$num_results."</p>";

    for($i = 0; $i < $num_results; $i++)
    {
        $row = $result->fetch_assoc();
        //$row = mysqli_fetch_assoc($result);
        echo "<p><strong>".($i+1).". Part Number: ";
        echo htmlspecialchars(stripslashes($row['Part Number']));
        echo "</strong><br /> Alternate Part Number: ";
        echo stripslashes($row['Alternate Part Number']);
        echo "<br />Qty: ";
        echo stripslashes($row['Qty']);
        echo "<br />Description: ";
        echo stripslashes($row['Description']);
        echo "<br />Part Condition: ";
        echo stripslashes($row['Part Condition']);
        echo "<br />Price: ";
        echo stripslashes($row['Price']);
        echo "<br />Location: ";
        echo stripslashes($row['Location']);
        echo "<br />Barcode: ";
        echo stripslashes($row['Barcode']);
        echo "<br />Consignment: ";
        echo stripslashes($row['Consignment']);
        echo "</p>";

    }

    $result->free();
    //mysqli_free_result($result);
    $db->close();

    ?>
4

2 回答 2

0

您能否发布以下代码的输出:

var_dump(get_object_vars($result));

似乎您的 $db 由于放错了@而为空

于 2013-01-12T22:14:28.130 回答
0

你的 "$db"-object 似乎是空的,因为你写了这个:

@ $db = new mysqli('localhost', 'username', 'password', 'partstest');

但是 @ 在这里是错误的,你也应该查看错误php-doc并对你的对象做一些“回声”,看看它们是假的还是空的

于 2013-01-12T22:14:33.227 回答