我有一个搜索查询,可以在表格中返回结果 - 如果没有结果,则返回文本无结果
然而,表格就像单元格中没有值一样?
根据搜索,该表具有不同的行数。
为什么要这样做?
编辑:这是php文件的完整代码
<?php
mysql_connect("x", "x", "x") or die("Error connecting to database: ".mysql_error());
mysql_select_db("x") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
$query = $_GET['query'];
$min_length = 0;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT s.*
, t.*
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE '%$query%' OR s.PalletSpace LIKE '%$query%';") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table border='1'>
<tr>
<th>Product Code</th>
<th>Pallet Space</th>
<th>Quantity</th>
<th>Date</th>
</tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['t.ProductName'] . "</td>";
echo "<td>" . $row['s.PalletSpace'] . "</td>";
echo "<td>" . $row['t.Quantity'] . "</td>";
echo "<td>" . $row['t.TransactionDate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
</body>
</html>
答案 0 :(得分:1)
试试这个。
<?
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "<td>" . $row['PalletSpace'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['TransactionDate'] . "</td>";
echo "</tr>";
}
?>
如果出现问题,数组索引使用小写。
答案 1 :(得分:1)
将您的查询更改为:
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace,(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE '%$query%' OR s.PalletSpace LIKE '%$query%';
然后更改字段名称,例如与SQL Query中的别名匹配。
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['Pname'] . "</td>";
echo "<td>" . $row['PSpace'] . "</td>";
echo "<td>" . $row['Qty'] . "</td>";
echo "<td>" . $row['Transac'] . "</td>";
echo "</tr>";
}
答案 2 :(得分:0)
请优化您的查询:
SELECT t.ProductName as tProductName, t.Quantity as tQuantity, t.TransactionDate as tTransactionDate, s.PalletSpace as sPalletSpace FROM PalletSpaces s JOIN ProductTrans t ON s.PalletSpaceID = t.PalletSpace WHERE t.ProductName LIKE '%$query%' OR s.PalletSpace LIKE '%$query%'
请遵循此伪代码:
$results = mysql_query( $sql, $connectionObject );
if ($results){ //some results are returned from query.
echo "<table>
<tr>
<th>Product Code</th>
...
</tr>";
while($row = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo " <td>" . $row['tProductName'] . "</td>";
echo " <td>" . $row['sPalletSpace'] . "</td>";
echo " <td>" . $row['tQuantity'] . "</td>";
echo " <td>" . $row['tTransactionDate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{ //no results returned from query.
echo "No Results";
}
$results = mysql_query( $sql, $connectionObject );
if ($results){ //some results are returned from query.
echo "<table>
<tr>
<th>Product Code</th>
...
</tr>";
while($row = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo " <td>" . $row['tProductName'] . "</td>";
echo " <td>" . $row['sPalletSpace'] . "</td>";
echo " <td>" . $row['tQuantity'] . "</td>";
echo " <td>" . $row['tTransactionDate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{ //no results returned from query.
echo "No Results";
}
希望,您将从上面的代码中获得核心思想。