【发布时间】:2015-09-04 00:13:14
【问题描述】:
我正在尝试在表格中显示 mysql_fetch_array() 结果。
我想显示guests name、their 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 一个低于另一个。同样,我也到了countries 和agreed 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扩展已弃用,考虑使用mysqli或PDO -
@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