向变量添加额外数据

时间:2010-12-31 09:49:24

标签: php variables

现在,我的代码使用Mysql只取出一个值。所以我想我也可以将每个找到的结果添加到变量中,但我不知道该怎么做。

这必须是非常基本问题,但我无法找到答案

    echo '<table border="1">';
  echo "<tr><td><b>Surname</b></td><td><b>Title/Name</b></td><td><b>Numbers</b></td><td><b>Telephone</b></td><td><b>Edit</b></td><td><b>Del</b></td></tr>\n";
  while ($row= mysql_fetch_array($result)) {
  $Surname = $row["Surname"];
  $Title = $row["TitleName"];
  $Email = $row["Email"];
  $Telephone = $row["Telephone"];
  $id = $row["id"];
  $MooringNumbers = $row['Number'];  
  $Assignedto['AssignedTo'];  
  }
  $MooringQuery = "select * FROM mooring WHERE AssignedTo='$id'";
  $MooringResult = mysql_query($MooringQuery) or die("Couldn't execute query");
  while ($row1= mysql_fetch_array($MooringResult)) {
  $AssignedTo = $row1["AssignedTo"];
  $MooringNumbers = $row1["Number"];
    echo '<tr><td>' .$Surname.'</td><td>'.$Title.'</td><td>'.$MooringNumbers . '</td><td>'.$Telephone.'</td><td>' . '<a href="rlayCustomerUpdtForm.php?id='.$id.'">[EDIT]</a></td>'.'<td>'. '<a href="deleteCustomer.php?id='.$id.'">[x]</a></td>'. '</tr>';   
}

2 个答案:

答案 0 :(得分:0)

$rows = array();
while($row = mysql_fetch_array($dbResult)) {
    $rows[] = $row;
}

您可以使用$rows

遍历foreach
foreach($rows as $row) {
    // do something with $row
}

如果您使用PDO进行数据库访问,您还可以在结果对象上使用fetchAll()方法,该方法在上面的代码中返回类似于$rows的数组。

答案 1 :(得分:0)

也许我误解了这个问题,但答案听起来就像将最后几行放入外while循环一样简单:

while ($row= mysql_fetch_array($result)) {
  $Surname = $row["Surname"];
  $Title = $row["TitleName"];
  $Email = $row["Email"];
  $Telephone = $row["Telephone"];
  $id = $row["id"];
  $MooringNumbers = $row['Number'];  
  $Assignedto['AssignedTo'];  
  $MooringQuery = "select * FROM mooring WHERE AssignedTo='$id'";
  $MooringResult = mysql_query($MooringQuery) or die("Couldn't execute query");
  while ($row1= mysql_fetch_array($MooringResult)) {
    $AssignedTo = $row1["AssignedTo"];
    $MooringNumbers = $row1["Number"];
    echo '<tr><td>' .$Surname.'</td><td>'.$Title.'</td><td>'.$MooringNumbers . '</td><td>'.$Telephone.'</td><td>' . '<a href="rlayCustomerUpdtForm.php?id='.$id.'">[EDIT]</a></td>'.'<td>'. '<a href="deleteCustomer.php?id='.$id.'">[x]</a></td>'. '</tr>';   
  }
}