-1

我需要帮助,我尝试了获取的行,但无法将其附加到while循环中的数组中:$rows[] = $row;然后尝试稍后在 foreach 中构建$rows arraythen 输出。

我该怎么做呢?

 <?php
    $query = $dbc->query('SELECT * FROM customer');
    $rows = array();
    while ($row = $query->fetch_assoc()) {
        echo $row['clientid'].' '.$row['inital'].' '.$row['firstname'].' '.$row['lastname'].' '.$row['mobile'].' '.$row['landline'].' '.$row['email'].' '.$row['address'].' '.$row['postcode'];
    }
 foreach ($rows as $row) { 
 ?>
  <td><?php $row['clientid']; ?></td>
  <td><?php $row['inital']; ?></td> 
  <td><?php $row['firstname']; ?></td>
  <td><?php $row['lastname']; ?></td>
  <td><?php $row['mobile']; ?></td>
  <td><?php $row['landline']; ?></td>
  <td><?php $row['email']; ?></td>
  <td><?php $row['address']; ?></td>
  <td><?php $row['postcode']; ?></td>
  </td>
  <?php } ?>

最后我是这样做的

   <?php
  $query = $dbc->query('SELECT * FROM customer');
if ( mysqli_num_rows( $query ) > 0 )
{
  while ( $row = mysqli_fetch_array( $query, MYSQLI_ASSOC ))
  {
 echo "<tr><td>".$row['clientid']."</td><td>".$row['inital']."</td><td>".$row['firstname']."</td><td>".$row['lastname']."</td><td>".$row['mobile']."</td><td>".$row['landline']."</td><td>".$row['email']."</td><td>".$row['address']."</td><td>".$row['postcode']."</td></tr>";
  }
  echo '</tbody></table>' ;
}
else { echo '<div class="alert alert-error">
There are currently no records.
</div>' ; }
    ?>    
4

4 回答 4

1

我不知道你为什么想要 $rows 中的所有记录并重复使用 foreach 两次,但你仍然可以这样做,

$rows = array();
while ($row = $query->fetch_assoc()) {
   $rows[] = $row;
}
于 2013-02-20T10:47:06.463 回答
1

Your variable $rows is initialized as an array, but is not being used.

while ($row = $query->fetch_assoc()) {
    $rows[] = $row;
}

This will create an array of all your $row

于 2013-02-20T10:52:32.773 回答
0

请使用回车和制表符。

另外,为什么要构建 rows 数组?fetch_assoc 为你做这件事

<?php while ($row = $query->fetch_assoc()) { ?> 
    <td><?php echo $row['clientid']; ?></td> 
    <td><?php echo $row['inital']; ?></td> 
    <td><?php echo $row['firstname']; ?></td> 
    <td><?php echo $row['lastname']; ?></td> 
    <td><?php echo $row['mobile']; ?></td> 
    <td><?php echo $row['landline']; ?></td> 
    <td><?php echo $row['email']; ?></td> 
    <td><?php echo $row['address']; ?></td> 
    <td><?php echo $row['postcode']; ?></td> 
    <td><?php echo $row['accessibility']; ?></td> 
<?php } ?>

或者,如果您想分两步完成

$rows = $query->fetch_all();

...

<?php foreach ($rows as $row) { ?> 
    <td><?php echo $row['clientid']; ?></td> 
    <td><?php echo $row['inital']; ?></td> 
    <td><?php echo $row['firstname']; ?></td> 
    <td><?php echo $row['lastname']; ?></td> 
    <td><?php echo $row['mobile']; ?></td> 
    <td><?php echo $row['landline']; ?></td> 
    <td><?php echo $row['email']; ?></td> 
    <td><?php echo $row['address']; ?></td> 
    <td><?php echo $row['postcode']; ?></td> 
    <td><?php echo $row['accessibility']; ?></td> 
<?php } ?>
于 2013-02-20T10:49:22.107 回答
0

回答你的问题:

while ($row = $query->fetch_assoc()) {
    $rows[] = $row;
    echo $row['clientid'].' '.$row['inital'].' '.$row['firstname'].' '.$row['lastname'].' '.$row['mobile'].' '.$row['landline'].' '.$row['email'].' '.$row['address'].' '.$row['postcode'].' '.$row['accessibility'];
}

但是,您可以采取另一种方法来解决此问题。为什么不简单地使用while循环来打印行?

while ($row = $query->fetch_assoc()) {
?>
<td><?php $row['clientid']; ?></td>
<td><?php $row['inital']; ?></td> 
<td><?php $row['firstname']; ?></td>
<td><?php $row['lastname']; ?></td>
<td><?php $row['mobile']; ?></td>
<td><?php $row['landline']; ?></td>
<td><?php $row['email']; ?></td>
<td><?php $row['address']; ?></td>
<td><?php $row['postcode']; ?></td>
<td><?php $row['accessibility']; ?></td>
<?php } ?>

或者,如果您只需要数组中的行,您可以使用fetchAll(),如下所示:

$rows = $query->fetchAll(PDO::FETCH_ASSOC);
于 2013-02-20T10:49:59.143 回答