0

我正在尝试使用从数据库中获取的数据打印出一个表格。这是代码

<?php
  $host = "localhost";
  $user = "root";
  $pass = "";
  $databaseName = "caliban";
  $tableName = "caliban";
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  $result = mysql_query("SELECT * FROM $tableName");            //query
  $array = mysql_fetch_assoc($result);                          //fetch result   
  //--------------------------------------------------------------------------
  // 3) echo result as json
  //--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName");            //query 

$rows = Array();
$i=0;
while($row = mysql_fetch_assoc($result)){
        //array_push($rows, $row);
      $rows[$i++] = $row;
}
for($j=0;$j<count($rows); $j++){
      echo
      "<table><tbody><tr id='$rows[$j]['id']'>
<td><input type='checkbox' /></td>
<td>$rows[$j]['firstname']</td>
<td>$rows[$j]['lastname']</td>
<td>$rows[$j]['city']</td>
<td>$rows[$j]['continent']</td>
</tr></tbody></table>";
}
?>

错误重复 8 次,因为这些是我拥有的总行数。

4

3 回答 3

1

为什么你做的那么复杂?

请看一下本教程: http: //php.net/manual/en/function.mysql-fetch-assoc.php

也许您还考虑切换到 MySQLI,因为 MySQL 已被弃用

于 2013-05-29T09:14:06.657 回答
0

这就是我修复它的方法

<?php
  $host = "localhost";
  $user = "root";
  $pass = "";
  $databaseName = "caliban";
  $tableName = "caliban";
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  $result = mysql_query("SELECT * FROM $tableName");            //query
  $array = mysql_fetch_assoc($result);                          //fetch result   
  //--------------------------------------------------------------------------
  // 3) echo result as json
  //--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName");            //query 

$rows = Array();
$i=0;
while($row = mysql_fetch_assoc($result)){
        //array_push($rows, $row);
      $rows[$i++] = $row;
}
for($j=0;$j<count($rows); $j++){
$id = $rows[$j]['id'];
$firstname = $rows[$j]['firstname'];
$lastname = $rows[$j]['lastname'];
$city = $rows[$j]['city'];
$continent = $rows[$j]['continent'];
      echo 
      "<table><tbody><tr id='$id'>
<td><input type='checkbox' /></td>
<td>$firstname</td>
<td>$lastname</td>
<td>$city</td>
<td>$continent</td>
</tr></tbody></table>";
}
?>
于 2013-05-29T09:50:09.840 回答
0
$rows = Array();
$i = 0;
echo "<table><tbody>";
while($row = mysql_fetch_assoc($result)) {
  extract($row);
  ?>
  <tr id="<?php echo $id ?>" >
        <td><input type=checkbox /></td>
        <td><?php echo $firstname ?></td>
        <td><?php echo $lastname ?></td>
        <td><?php echo $city ?></td>
        <td><?php echo $continent ?></td>
      </tr>
      <? }

echo "</tbody></table>";
于 2013-05-29T09:39:35.967 回答