php 使用PHP foreach获取mysql表数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5003099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:31:05  来源:igfitidea点击:

Using PHP foreach to get mysql table data

phpmysqlforeach

提问by tonoslfx

ive got 1 database table contains row:

我有 1 个数据库表包含行:

TABLE FROM reservations:

来自预订的表格:

attandee01  
attandee02  
attandee03  
attandee04  
attandee05  
attandee06

PHP CODE:

代码:

$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   echo '<td>' . $r['attandee1'] . '</td>';
   echo '<td>' . $r['attandee2'] . '</td>'
   echo '<td>' . $r['attandee3'] . '</td>'
endwhile;

or is there any simple way using foreach to echo attandee1 - attandee10?

或者有什么简单的方法使用 foreach 来回应 attandee1 - attandee10?

回答by Dai

$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   foreach($r as $value) {
       echo '<td>' . $value . '</td>';
   }
endwhile;

Should echo each column value per table row.

应该对每个表行的每个列值进行回显。

EDIT: If you only want the attendees:

编辑:如果您只想要与会者:

$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   for($i = 1; $i < 11 $i++) {
       echo '<td>' . $r['attendee'.$i] . '</td>';
   }
endwhile;

回答by user268396

Well yes:

嗯,是:

for($i=1; $i<11; ++$i) {
  echo "<td>".$r["attandee".$i]. "</td>";
}

But that is columns, what you want is the row based solution for that I'd use something like:

但那是列,你想要的是基于行的解决方案,我会使用类似的东西:

for($r=$q->fetch_assoc(); !is_null($r); $r= $q->fetch_assoc()) {
   echo "<td>".array_pop($r)."</td>"; // output the only element in the array?
}
$q->free(); // don't forget to free the memory of the result set!

回答by Your Common Sense

you can use foreach, of course, as $ris a regular array and can be iterated using foreach() operator. Did you try it?

当然,您可以使用 foreach,就像$r常规数组一样,并且可以使用 foreach() 运算符进行迭代。你试了吗?

However, looking at the field names, I suspect serious design flaw in your data structure.
It seems attandees should be stored in another table.

但是,查看字段名称,我怀疑您的数据结构存在严重的设计缺陷。
似乎参加者应该存储在另一个表中。

You may also consider using templates. Printing data directly from the database loop is very bad practice. You have to get all your data first and only then start printing it out.

您也可以考虑使用模板。直接从数据库循环打印数据是非常糟糕的做法。您必须首先获取所有数据,然后才开始打印出来。

回答by oezi

don't know if i understood your question... is this what you're looking for:

不知道我是否理解你的问题......这就是你要找的:

while($row = $q->fetch_array(MYSQLI_ASSOC)):
    foreach($row as $field){
       echo '<td>' . $field . '</td>';
    }
endwhile;

回答by obohovyk

As example you can use alternate syntax.

例如,您可以使用替代语法。

<table>
<?php foreach ($mysqli->query($sql) as $row ): ?>
<tr><td><?php echo $row['row_name1']; ?></td><td><?php echo $row['row_name2']; ?></td></tr>
<?php endforeach; ?> 
</table>

回答by samy

Not really sure if this is what you are looking for, but give it a shot:

不确定这是否是您要找的东西,但试一试:

while($r = $q->fetch_array(MYSQLI_ASSOC)) {
   foreach($r as $value) {
      echo '<td>' . $value . '</td>';
   }
}