foreach获取mysql表数据

foreach获取mysql表数据

本文介绍了使用PHP foreach获取mysql表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





  attandee01 
attandee02
attandee03
attandee04
attandee05
attandee06

PHP代码:

  $ q = $ db-> query(SELECT * FROM bs_events 
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id); ($ r = $ q-> fetch_array(MYSQLI_ASSOC)):
echo'< td>'。 $ r ['attandee1']。 < / TD>;
echo'< td>'。 $ r ['attandee2']。 '< / td>'
回声'< td>'。 $ r ['attandee3']。 '< / td>'
endwhile;

或者有没有简单的方法使用foreach回声attandee1 - attandee10?

解决方案
  $ q = $ db-> query(SELECT * FROM bs_events 
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id);
$ b $ while($ r = $ q-> fetch_array(MYSQLI_ASSOC)):
foreach($ r as $ value){
echo'< td>'。 $值。 < / TD>;
}
endwhile;

应该为每个表行回显每列值。

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

  $ q = $ db-> query(SELECT * FROM bs_events 
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id);
$ b $ while($ r = $ q-> fetch_array(MYSQLI_ASSOC)):
for($ i = 1; $ i echo' < td>'。 $ r ['attendee'。$ i]。 < / TD>;
}
endwhile;


ive got 1 database table contains row:

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?

解决方案
$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;

这篇关于使用PHP foreach获取mysql表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:07