如何显示查询结果 [英] how to display result of query
本文介绍了如何显示查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查询后,我尝试显示数据.我只能从field_1[]"接收数据.来自'field_2[]' 和来自'field[]' 没有.如何解决?
After query I try to display data. I can receive only data from 'field_1[]'. From 'field_2[]' and from 'field[]' no. How to fix it?
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_rows($result);
//------------------------------------------------------------------
for($i_1=0; $i_1<$fields_num; $i_1++)
{
$field_1 = mysql_fetch_assoc($result);
echo "<td>a".$field_1['index_period_1']."</td>";
}
//------------------------------------------------------------------
//------------------------------------------------------------------
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_assoc($result);
echo "<td>b".$field['index_period']."</td>";
}
//------------------------------------------------------------------
//------------------------------------------------------------------
for($i_2=0; $i_2<$fields_num; $i_2++)
{
$field_2 = mysql_fetch_assoc($result);
echo "<td>c".$field_2['index_period_2']."</td>";
}
----------------------
edit:----------------------
|------------|period_1 |period_1 |period_1 |
-----------------------------------------------
|period_2 |period |period |period |
-----------------------------------------------
|period_2 |period |period |period |
-----------------------------------------------
推荐答案
你有点忽略了 mysql_fetch_assoc()
和 MySQL 中的行的要点:
You are sort of missing the point of mysql_fetch_assoc()
and rows in MySQL:
while ($row = mysql_fetch_assoc($result)) {
echo $row['index_period'];
echo $row['index_period_1'];
echo $row['index_period_2'];
}
每行调用mysql_fetch_assoc()
一次.
我不确定为什么你需要像这样遍历你的桌子,但我不会审问你.
I'm not really sure why you need to loop over your table like this, but I won't interrogate you.
这可能符合您的需求(我不敢写这个):
This might fit your needs (I cringe writing this):
$index_period = array();
$index_period_1 = array();
$index_period_2 = array();
while ($row = mysql_fetch_assoc($result)) {
$index_period[] = $row['index_period'];
$index_period_1[] = $row['index_period_1'];
$index_period_2[] = $row['index_period_2'];
}
foreach ($index_period as $value) {
echo "<td>a" . $value . "</td>";
}
foreach ($index_period_1 as $value) {
echo "<td>b" . $value . "</td>";
}
foreach ($index_period_2 as $value) {
echo "<td>c" . $value . "</td>";
}
这篇关于如何显示查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文