dqlk31541 2014-03-10 22:53
浏览 29
已采纳

如何使用PHP从MySql查询中显示多个结果

I have my current code working perfectly but I needed help on when I want the code to display as many queries exist in the table called wine... for example if there are more than wine type in category as spain then it should display all but it only displays one.

Here is my PHP:

<?php  

include"db_connection.php";

$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");
    while($row = mysql_fetch_array($sql)){ 
         $description = $row["description"];
         $wine_type = $row["wine_type"];
         $country = $row["country"];
         $bottle_price = $row["bottle_price"];
         $indicator = $row["indicator"];
         $colour = $row["colour"];
         $case_price = $row["case_price"];
         $case_size = $row["case_size"];         
         $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));    
     }
?>

Here is my HTML:

<?php include('header.php'); ?>

<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
    <tr>
        <td width="19%" valign="top"><img src="inventory_images/<?php echo $id; ?>.jpg" width="142" height="188" alt="<?php echo $wine_type; ?>" /><br />
        <a href="inventory_images/<?php echo $id; ?>.jpg">View Full Size Image</a></td>
            <td width="81%" valign="top"><h3><?php echo $wine_type; ?></h3>
            <p><?php echo "$".$bottle_price; ?><br /><br />
                <?php echo "$country $indicator"; ?> <br /><br />
                <?php echo $description; ?> <br />
            </p>

              <form id="form1" name="form1" method="post" action="cart.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Shopping Cart" />
              </form>
            </td>
    </tr>
</table>
</div>

<?php include('footer.php'); ?>
  • 写回答

1条回答 默认 最新

  • dsstjqsr631426 2014-03-10 22:57
    关注

    The problem is currently that the while() loop will keep overriding the variables. This can be solved in a couple of ways, one would be that you save the entire fetched column in an array and use that array later for iteration.

    $allRows = array();
    while($row = mysql_fetch_array($sql)) {
        $allRows[] = $row;
    }
    

    Now, as mentioned above, iterate over $allRows in your template

    <?php include('header.php'); ?>
    <div id="content">
    <table width="100%" border="0" cellspacing="0" cellpadding="15">
    <?php
    foreach ($allRows as $row) {
    ?>
        <tr>
          <td width="19%" valign="top"><img src="inventory_images/<?php echo $row['id']; ?>.jpg" width="142" height="188" alt="<?php echo $row['wine_type']; ?>" /><br />
          etc.
        </tr>
    <?php
    }
    ?>
    </table>
    </div>
    <?php include('footer.php'); ?>
    

    Here I addressed the variables as $row['...'] - if you don't want to change that part of the code, simply do the assigning at the beginning of the loop.

    etc.
    <?php
    foreach ($allRows as $row) {
         $description = $row["description"];
         $wine_type = $row["wine_type"];
         //etc.
    ?>
    

    A much cleaner solution (not mixing that much HTML and PHP which usually creates lots of confusion) would be using a template engine. Also don't use mysql_* functions in new code - they are deprecated. See this answer for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog