【问题标题】:How to show mysql multi row / mysql_fetch_array results in a table?如何在表中显示 mysql 多行/mysql_fetch_array 结果?
【发布时间】:2015-09-04 00:13:14
【问题描述】:

我正在尝试在表格中显示 mysql_fetch_array() 结果。

我想显示guests nametheir country 和他们的agreed time 同一日期

以下代码工作正常。代码获取行值并打印出来。

 $select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
    echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
    echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
    echo $row['agreed_time'].'<br/>';
}

如果同一日期有 5 位客人,当我执行上述代码时,我会得到他们所有的 names 一个低于另一个。同样,我也到了countriesagreed time

现在我想在 HTML 表格中显示这些结果。 我尝试了几行代码,但没有任何效果。

我的 HTML 表格应该如下:

<table class="table-fill">
    <thead>
        <tr>
            <th class="text-left">Name</th>
            <th class="text-left">From</th>
            <th class="text-left">Agreed Time</th>
        </tr>
    </thead>
    <tbody class="table-hover">
        <tr>
            <td class="text-left">Name 1</td>
            <td class="text-left">Country 1</td>
            <td class="text-left">Ag Time 1</td>
        </tr>
        <tr>
            <td class="text-left">Name 2</td>
            <td class="text-left">Country 2</td>
            <td class="text-left">Ag Time 2</td>
        </tr>
        <tr>
            <td class="text-left">Name 3</td>
            <td class="text-left">Country 3</td>
            <td class="text-left">Ag Time 3</td>
        </tr>
        <tr>
            <td class="text-left">Name 4</td>
            <td class="text-left">Country 4</td>
            <td class="text-left">Ag Time 4</td>
        </tr>
        <tr>
            <td class="text-left">Name 5</td>
            <td class="text-left">Country 5</td>
            <td class="text-left">Ag Time 5</td>
        </tr>
    </tbody>
</table>

如何根据我的 mysql_fetch_array() 创建那个表 td ? 上面的表结构是为5 guests找到的或由mysql_fetch_array()产生的

【问题讨论】:

  • mysql 扩展已弃用,考虑使用mysqliPDO
  • @Muhammet 谢谢.. 注意到.. DOWN VOTERS :我在这里问我什么时候不明白/不知道什么......所以当有人真的想DOWN VOTE请至少评论原因..我不够聪明,无法理解为什么我投了反对票..
  • 如果可以的话,你应该stop using mysql_* functions。它们不再被维护并且是officially deprecated。改为了解 prepared statements,并考虑使用 PDO,it's really not hard

标签: php mysql arrays while-loop html-table


【解决方案1】:

首先,我认为您的解决方案不需要 3 个不同的查询..

    <table class="table-fill">
            <thead>
                <tr>
                    <th class="text-left">Name</th>
                    <th class="text-left">From</th>
                    <th class="text-left">Agreed Time</th>
                </tr>
            </thead>
        <?php 
        $result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); 
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
        { 
        ?>
          <tr>
               <td>
                   <?php    echo $row['name']; ?>
               </td>
               <td>
                   <?php     echo $row['country'];?>
               </td>
                <td>
                   <?php     echo $row['agreed_time']; ?>
               </td>    
    </tr>
    <?php
    }
?>
</table>

【讨论】:

    【解决方案2】:

    首先,你应该使用mysqli

    其次,不应该向数据库发送如此多的查询以获取您可以在一次查询中获得的信息;

    $select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
    

    接下来,你要fetch the number of rows

    $rows = $mysqli
    

    您还应该查看PHP for function

    for ($i = 1; $i < $rows; $i++) {
        $thisRow = $select_guests->fetch_row()
        echo
    '        <tr>
            <td class="text-left">'.$select_guests['name'].'</td>
            <td class="text-left">'.$select_guests['country'].'</td>
            <td class="text-left">'.$select_guests['time'].'</td>
        </tr>
        '; //This last line is to insert a line break and indent (for tidy HTML)
    }
    

    试一试,希望对您有所帮助。 不过,我还没有为你完全解决它,为了切换到 mysqli,你需要做一些小改动,你可以在我发给你的 mysqli 链接中找到这些改动。这些好处是值得的。

    【讨论】:

    • 谢谢你..我会再试一次..我已经在使用 Habib Ul haq 的建议..我支持你..你的回答很整洁..
    • 谢谢。我感谢您的赞成票和您的回应。祝你好运。
    【解决方案3】:
    $select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
    while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
        <tr>
               <td>
                   <?php    echo $row['name']; ?>
               </td>
               <td>
                   <?php     echo $row['country'];?>
               </td>
                <td>
                   <?php     echo $row['agreed_time']; ?>
               </td>    
      </tr>
    
    }
    

    【讨论】:

    • 这没有回答问题。 OP 想要填充表格的行。
    • 我认为这给出了这个想法。但我还是做了编辑
    • 为什么 OP 应该这样做?请添加对您所做的事情以及为什么这样做的解释,不仅是为了 OP,也是为了未来的 SO 访问者。
    猜你喜欢
    • 2020-02-22
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多