显示多行MySQL

时间:2013-08-14 16:05:44

标签: php mysql

我的搜索结果的编码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 个答案:

答案 0 :(得分: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> ";
  }
}
?>

答案 1 :(得分: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> ";
}

这仍然非常混乱,甚至更糟,deprecated。尝试切换到mysqli_ *函数或PDO。考虑在类中封装代码,或者至少考虑清理一些东西。

答案 2 :(得分:2)

首先你需要找到$ result ... $numberofrows = mysql_num_rows($result)中有多少行代码,然后创建循环,例如for ($i =0; $i<$numberofrows; $i++)然后你需要回显你得到的所有行周期

顺便说一下...使用函数mysql_ *已弃用,所以你宁愿使用PDO或mysqli ......

答案 3 :(得分:0)

在while块中执行$ 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> ";
    }
}

?>