0

我正在尝试使用LIKE来自用户输入的查询检索所有数据并将其与数据库匹配,它可以工作但只返回一条记录,但我在表中有很多记录。

它返回它可以找到的最近的记录,

比如说我有 2 条记录,他们的 ItemDesc 字段包含字符“The”,当我在输入框中搜索“The”并单击提交时,它会返回最接近(最早创建)的记录,而它应该返回两者。

<?php
$username = "a3355896_guy";
$password = "++++++";
$hostname = "mysql5.000webhost.com";    
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to      MySQL");

mysql_select_db("a3355896_book") or die("Unable to connect to database");

$ItemDesc = $_POST['ItemDesc'];
$query = "select * from StockItems where ItemDesc LIKE '%$ItemDesc%'"; 
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>

抱歉应该包括检索:

<?php
if ($num>0)
{
echo "<center><table border=1><tr><th>Item Code</th><th>Item Desc</th>";
echo "<th>Item Stock Qty</th>";
echo "<th>Item Unit Price</th><th>Item Category</th></tr>";
$ItemCode = mysql_result($result,$i,"ItemCode");
$ItemDesc = mysql_result($result,$i,"ItemDesc");
$ItemStockQty = mysql_result($result,$i,"ItemStockQty");
$ItemUnitPrice = mysql_result($result,$i,"ItemUnitPrice");
$ItemCategory = mysql_result($result,$i,"ItemCategory");
echo "<tr><td>$ItemCode</td><td>$ItemDesc</td><td align=right>";
echo "$ItemStockQty</td>";
echo "<td align=right>$ItemUnitPrice</td>";
echo "<td>$ItemCategory</td></tr>";
echo "</table></center>";

}
else
{
echo "<form name='DeleteStock2'>";
echo "<p> Sorry, $ItemDesc does not exist!<p>";
echo "<input type='button' value='Leave' onclick='history.go(-1)'>";
}

?>

4

1 回答 1

1

您实际上并没有在这里访问您的数据——您需要遍历结果集。

$setLength = mysql_num_rows($result);
for($i = 0; $i < $setLength; $i++){
    //Here, mysql_fetch_assoc automatically grabs the next result row on each iteration
    $row = mysql_fetch_assoc($result);
    //do stuff with "row"
}

除非您正在这样做并且您只是选择不将其包含在您的代码段中。让我们知道:)

--Edit-- 首先,我道歉-出于旧习惯,我建议您使用 mysql_fetch_assoc 而不是 mysqli 函数集。

尝试使用 fetch_assoc 或 fetch_array 函数,它可以解决您的问题。我从未使用过您使用的方法,我认为它已经被弃用了一段时间。在这里查看:http: //php.net/manual/en/mysqli-result.fetch-assoc.php

于 2013-05-22T20:04:37.830 回答