我的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 Development 所以我正在使用他们的一部分代码。
以下是代码:
<?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();
?>
答案 0 :(得分:0)
请您发布以下代码的输出:
var_dump(get_object_vars($result));
由于错误的@
,似乎你的$ db是空的答案 1 :(得分:0)
你的“$ db”对象似乎是空的,因为你写了这个:
@ $db = new mysqli('localhost', 'username', 'password', 'partstest');
但@在这里是错误的,你也应该查看错误php-doc并对你的对象做一些“回声”,看它们是假的还是空的