我正在尝试制作一个简单的搜索引擎,并且结果显示为正常。我唯一的问题是我希望它能更加出色。
这是它的代码(我还有另一个.php,它可以获得搜索,搜索功能和jQuery(
<?php
mysql_connect ("localhost","root","xxxxxxx") or die ("Connectionissues");
mysql_select_db ("xxxxxxxx") or die("Can't find database");
$output = '';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#^0-9a-z#^1"," ",$searchq);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE
'%$searchq%'") or die("Search incomplete!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Order have never been made before';
}else{
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
$output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.'
</div>';
}
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
}
echo ($output);
?>
为了限制我尝试在这样的while语句之前进行if语句的搜索:
:if($count = <100){
$output = 'Too many results!';
对于表格,我尝试了各种方法,最终我总是制作简单的HTML表,但是我无法将搜索结果发布在四列中。
第一,您应该真正考虑使用PPS:准备的参数化语句。这将有助于防止SQL注入
如果要限制和订购,请使用查询,因此,mysql:limit of QUERY优化很有用。
对于您的要求,使用类似的内容:
<?php
-> SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' ORDER BY title ASC, quantity DESC LIMIT 20
// THIS IS NOT SAFE as you trust user data !!!
// then you have 20 results already ordered
// here, I used title alphabetical order + the most avalaible quantity, but you adapt it...
if ($result_of_num_rows > 0) { /* we have results */
echo"<table>"; // only raw, use a nicely formatted html output :)
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
echo"<tr>
<td> $idorder </td>
<td> $idproduct </td>
<td> $title </td>
<td> $qty </td>
</tr>";
}
echo"</table>";
}
else { echo"nothing yet !"; }
?>
更好的是使用"新"标准选择API,您在下面有一个示例(我希望(可以帮助您选择新路径:(
<?php
error_reporting(E_ALL); ini_set('display_errors', 1); /* PHP will help us */
/* connexion to db */
$mysqli = mysqli_connect("$host", "$user", "$pwd", "$db");
if (mysqli_connect_errno()) { echo "Error connecting to DB : " . mysqli_connect_error($mysqli); }
$param = "%{$_POST['searchq']}%";
$query = " SELECT idProduct, idOrder, title, qty FROM ds_OrderItem WHERE idProduct LIKE ? ORDER BY title ASC, quantity DESC LIMIT 20 ";
$stmt = $mysqli->prepare($query); /* prepare query */
$stmt->bind_param("s", $param); /* bind param wil sanitize */
print_r($stmt->error_list); /* check for error -> can be removed later */
print_r($stmt->get_warnings()); /* check for error -> can be removed later */
print_r($stmt->error); /* check for error -> can be removed later */
$results = $stmt->execute(); /* execute query */
$stmt->bind_result($idProduct, $idOrder, $title, $qty); /* bounded results */
$stmt->store_result();
if ($stmt->num_rows > 0) { /* we have results */
echo"<table>"; // start table
while($stmt->fetch()){ /* loop through results */
echo"<tr>
<td> $idOrder </td>
<td> $idProduct </td>
<td> $title </td>
<td> $qty </td>
</tr>";
}
echo"</table>";
}
else
{ echo"[ no data ]"; }
?>
您可能的解决方案在下面,您可以在关闭之前对变量进行计数,并在
关闭之前将条件放在循环中 <table>
<?php while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
?>
<tr>
<td><?php echo $idproduct; ?></td>
<td><?php echo $idorder; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $qty; ?></td>
</tr>
<?php } ?>
</table>