如何使用while循环获取详细信息,并使用foreach〔MySQL和PHP〕进行显示


How to fetch details with while loop and display with foreach[MySQL and PHP]

如何使用while循环获取细节并使用foreach显示?需要使用绑定来完成此操作。。。。

$stmt= $db ->prepare("SELECT quote_autor, quote_text FROM quotes")or die($db->error);
if( $stmt !== FALSE ) {
$stmt->execute();
$stmt->bind_result($quote_autor,$qupte_text);
$records= array();
while($stmt ->fetch()){
$records[]=$stmt ->fetch();
///??????
}
foreach($records as $rec){
//???????
}

使用while获取记录:

while($records[] = $stmt ->fetch());

取后使用for-each循环通过

foreach($records[] as $record) {
    echo $record['COLUMN_NAME'];
    // use var_dump or print_r to know more
}

编辑:

如果你想打印所有的名字,而不是使用for-each,你可以做:

echo implode(", ", $array);
$stmt= $db ->prepare("SELECT quote_autor, quote_text FROM quotes")or die($db->error);
if( $stmt !== FALSE ) {
$stmt->execute();
$stmt->bind_result($quote_autor,$qupte_text);
$records= array();
while($dyna=$stmt->fetch()){
    array_push($records,$dyna);
}
echo "<table>";
foreach($records as $rec){
    echo "<tr><td><b>Autor: ".$row[0]."</b></td><td><p>".$row[1]."</td></tr>";
}
echo "</table>";