我正在为一个作业编写一个简单的PHP页面,其中一个标准是同时使用mysqli_fetch_assoc和MysqLi_fetch_row.我对两者使用相同的查询:
<?PHP
// Perform database query
$query = "SELECT * FROM player JOIN stat ON player.playerId = stat.playerId";
$result = MysqLi_query($dbconnection, $query);
if (!$result) {
die("Database query Failed");
}
?>
当我在我的数据库中运行此查询时,它按预期返回3行.在我的网页中,我首先使用MysqLi_fetch_assoc($result),然后按预期呈现包含信息的无序列表.然后我继续使用MysqLi_fetch_row($result)来显示更多信息,但第二个while循环不会产生任何表数据(只是th标签的内容).
<h1>The Players</h1>
<ul>
<?PHP
// Use return data with MysqLi_fetch_assoc
while($row = MysqLi_fetch_assoc($result)) {
// output data from each row
?>
<li><?PHP echo $row["playerId"]; ?></li>
<li><?PHP echo $row["fname"] . " " . $row["lname"]; ?></li>
<li><?PHP echo $row["team"]; ?></li>
<li><?PHP echo $row["position"]; ?></li>
<?PHP echo "<hr />";
}
?>
</ul>
<h1>The Stats</h1>
<table>
<th>Player</th>
<th>Batting Avg</th>
<th>Homeruns</th>
<th>RBIs</th>
// DATA BELOW THIS POINT IS NOT RENDERED BY THE WEBPAGE
<?PHP
// Use return data with MysqLi_fetch_row
while($row = MysqLi_fetch_row($result)) {
?>
<tr>
<td><?PHP echo $row[2]; ?></td>
<td><?PHP echo $row[8]; ?></td>
<td><?PHP echo $row[9]; ?></td>
<td><?PHP echo $row[10]; ?></td>
</tr>
<?PHP
}
?>
</table>
<?PHP
// Release returned data
MysqLi_free_result($result);
?>
在接下来的几周里,我将为这门课写很多PHP,所以我真的很喜欢一些如何解决这类错误的技巧.有没有我可以使用的方法来轻松检查$result的内容,看看是否有任何资源实际通过?在我看来,第二个循环中的$row没有被分配给任何东西,所以它只是不执行echo命令. 解决方法: 幽灵是对的.当您在结尾处迭代记录时,它不会被itselt重新设置,并且它不是循环列表.因此,您必须使用$obj-> data_seek(int,即0)手动将其设置为0;或以程序风格MysqLi_data_seek($result,0); (编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|