为什么这个文件没有循环数据?它仅显示最后一个(或“最近的”)记录。 [我应该很明显我是PHP的新手]
<head>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
}
-->
</style>
</head>
<br />
<a href="add.php">Add entry</a><br>
<br>
<?php
include("connect.php");
$result = mysql_query("SELECT * FROM qual");
$num = mysql_num_rows($result);
if ($num > 0 ) { $i=0;
while ($i < $num) {
$id = stripslashes(mysql_result($result,$i,"id"));
$field1 = stripslashes(mysql_result($result,$i,"field1"));
$field2 = stripslashes(mysql_result($result,$i,"field2"));
$field3 = stripslashes(mysql_result($result,$i,"field3"));
$field4 = stripslashes(mysql_result($result,$i,"field4"));
$field5 = stripslashes(mysql_result($result,$i,"field5"));
$field6 = stripslashes(mysql_result($result,$i,"field6"));
$field7 = stripslashes(mysql_result($result,$i,"field7"));
$field8 = stripslashes(mysql_result($result,$i,"field8"));
$field9 = stripslashes(mysql_result($result,$i,"field9"));
$field9 = stripslashes(mysql_result($result,$i,"field9"));
$field10 = stripslashes(mysql_result($result,$i,"field10"));
$field11 = stripslashes(mysql_result($result,$i,"field11"));
$field12 = stripslashes(mysql_result($result,$i,"field12"));
$field13 = stripslashes(mysql_result($result,$i,"field13"));
$row = '<tr>
<td>'.$num.'</td>
<td>'.$field1.'</td>
<td>'.$field2.'</td>
<td>'.$field3.'</td>
<td>'.$field4.'</td>
<td>'.$field5.'</td>
<td>'.$field6.'</td>
<td>'.$field7.'</td>
<td>'.$field8.'</td>
<td>'.$field9.'</td>
<td>'.$field9.'</td>
<td>'.$field10.'</td>
<td>'.$field11.'</td>
<td>'.$field12.'</td>
<td>'.$field13.'</td>
<td><a href="update.php?id='.$id.'">Update</a></td>
<td><a href="delete.php?id='.$id.'">Delete</a></td>
</tr>';
++$i; }} else
{ $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; }
mysql_close();
?>
<table width="150%" border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" align="center">
<tr>
<td>field1</td>
<td>field2</td>
<td>field3</td>
<td>field4</td>
<td>field5</td>
<td>field6</td>
<td>field7</td>
<td>field8</td>
<td>field9</td>
<td>field10</td>
<td>field11</td>
<td>field12</td>
<td>field13</td>
<td> </td>
<td> </td>
</tr>
<?php echo $row ?>
</table>
答案 0 :(得分:2)
在if
条件行之前,初始化$row = "";
。
您必须连接if
和else
案例
不是$row = '<tr>
我应该
$row .= '<tr>
答案 1 :(得分:1)
一个。您永远不需要在数据库结果上运行stripslashes
,这意味着您错误地插入了数据。
B中。您应该切换到MySQLi / PDO,因为the original MySQL extension is now deprecated.
℃。您每次都在创建一个新的$row
,并且最后只回显一次。
d。你真的为你的领域命名field1
- field13
???
这可以写得那么简单。这是一个简单的PDO版本:
//Echo table header.
$sql = 'SELECT * FROM qual';
foreach ($conn->query($sql) as $row) {
// echo the row contents now.
}
答案 2 :(得分:1)
您在每次迭代时都会覆盖$row
。在循环中使用$row .= '<tr>...
来连接行。
答案 3 :(得分:0)
从:
开始<?php
include("connect.php");
你可以用这个:
$result = mysql_query("SELECT * FROM qual");
$toStamp = "";
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)){
$id = stripslashes($row["id"]);
$field1 = stripslashes($row["field1"]);
//etc
$toStamp = $toStamp.'<tr><td>'.$id.'</td> // Adding this line to the existing $row
<td>'.$field1.'</td>
//etc
} else {
$toStamp = '<tr><td colspan="2" align="center">Nothing found</td></tr>';
}
}
//编辑了一点,删除了一些代码错误。
答案 4 :(得分:-1)
您是否尝试在循环前显示$ num值,以检查您的查询确实有多于1个结果?