php – mysqli_fetch_row不返回结果,但是mysqli_fetch_assoc没有
发布时间:2020-12-13 16:55:58 所属栏目:PHP教程 来源:网络整理
导读:我正在为一个作业编写一个简单的php页面,其中一个标准是同时使用 mysqli_fetch_assoc和mysqli_fetch_row.我对两者使用相同的查询: ?php // Perform database query $query = "SELECT * FROM player JOIN stat ON player.playerId = stat.playerId"; $result
我正在为一个作业编写一个简单的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);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |