0

我的搜索结果编码 id。

我正在显示搜索结果,目前它只会显示第一个结果,我怎样才能让它显示与搜索匹配的所有结果?

这是我的代码

      <?php
    $name=$_GET["q"];
    if ($name<=""){echo( "");  
     }
     else
     {
    $con = mysql_connect("localhost","cl49-XXX","XXX");
    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().')' );

    $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='userpics/$prodID.jpg' height='80' width='80' 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</span></a>                                
    </div>
    </div> ";

   ?>

任何帮助都会很棒!

4

4 回答 4

3

像这样使用while循环:while ($row = mysql_fetch_array($result)) { }

<?php
$name = $_GET["q"];
if ($name <= "") {
  echo ("");
}
else {
  $con = mysql_connect("localhost", "cl49-XXX", "XXX");
  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() . ')');
  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='userpics/$prodID.jpg' height='80' width='80' 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</span></a>
    </div>
    </div> ";
  }
}
?>
于 2013-08-14T16:09:21.480 回答
2

你必须像这样遍历你的结果:

while($row = mysel_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='userpics/$prodID.jpg' height='80' width='80' 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</span></a>
 </div>
</div> ";
}

仍然这很混乱,甚至更糟糕的是,已弃用。尝试切换到 mysqli_* 函数或 PDO。并考虑在类中封装代码或至少在函数中进行一些清理工作。

于 2013-08-14T16:10:09.570 回答
2

首先,您需要找出 $result... 中有多少行代码$numberofrows = mysql_num_rows($result),然后进行循环,例如for ($i =0; $i<$numberofrows; $i++),然后您需要回显循环中获得的所有行

顺便说一句...使用函数 mysql_* 已被弃用,因此您宁愿使用 PDO 或 mysqli ...

于 2013-08-14T16:10:24.730 回答
0

在一段时间内做你的 $row 分配,像这样

<?php
$name=$_GET["q"];
if ($name<=""){echo( "");  
 }else {

 $con = mysql_connect("localhost","cl49-XXX","XXX");
    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().')' );

    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='userpics/$prodID.jpg' height='80' width='80' 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</span></a>                                
                </div>
                </div> ";
    }
}

?>
于 2013-08-14T16:15:24.093 回答