显示包含搜索条件的搜索结果

时间:2013-08-15 09:52:37

标签: php mysql search

我已编码搜索我的页面,但是我希望这能够显示包含搜索条件的产品...目前它只显示以cirteria开头的结果。

这是我的代码:

<?php
$name = $_GET["q"];
if ($name <= "") {
  echo ("");
}
else {
  $con = mysql_connect("localhost", "cl49-XXX", "vogalcms");
  if (!$con) {
    die('Could not connect: line 513 ' . mysql_error());
  }
mysql_select_db("cl49-XXX", $con) or die("Unable to select database");   
$result = mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ") or die('Error: Line 519 (' . mysql_error() . ')');
  mysql_select_db("cl49-vogalcms", $con) or die("Unable to select database");   


  while ($row = mysql_fetch_array($result)) {
    $prodID = $row['prodID'];
    $prodname = $row['prodname'];
    $catagory = $row['catagory'];
    echo "
     <div class='row-fluid portfolio-block'>
    <div class='span5 portfolio-text'>
    <img src='admin/userpics/$prodID.jpg' height='50' width='50' alt='' />
    <div class='portfolio-text-info'>
                                                <h4>$prodname</h4>
    <p></p>
        </div>
        </div>
    <div class='span5'>
<div class='portfolio-info'>
Product ID
                                                <span>$prodID</span>
        </div>
  <div class='portfolio-info'>
                                                catagory
                                                <span>$catagory</span>
 </div>
 </div>
  <div class='span2 portfolio-btn'>
  <a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View/Edit</span></a>
    </div>
    </div> ";
  }
}
?>

我希望能够搜索ca而不仅仅是显示CAr,CArpet还能显示nesCAfe吗?

2 个答案:

答案 0 :(得分:2)

只需将where子句更改为:

WHERE `prodname` LIKE '%$name%' 

答案 1 :(得分:1)

将搜索查询更改为

SELECT * FROM products WHERE `prodname` LIKE '%$name%'