当 foreach 后跟显示相同查询结果的 mysqli_fetch_row 时,mysqli_fetch_row() 不会显示。但是,如果 mysqli_fetch_row 在 foreach() 显示之前执行。为什么是这样?
询问:
<?php
$connection = mysqli_connect("localhost", "root", "", "login_app");
$query = "SELECT *
FROM users";
$result = mysqli_query($connection, $query);
if(!$result){
die("db connection failed " . mysqli_error());
}
?>
仅显示 FORACH 的结果:
<?php
foreach($result as $results){
echo "User ID: " . $results['userID'] . "<br>";
echo "Username: " . $results['userName'] . "<br>";
echo "Password: " . $results['password'] . "<br>";
};
$row = mysqli_fetch_row($result);
print_r($row);
?>
显示 FOREACH 和 MYSQLI_FETCH_ROW 的结果:
<?php
$row = mysqli_fetch_row($result);
print_r($row);
foreach($result as $results){
echo "User ID: " . $results['userID'] . "<br>";
echo "Username: " . $results['userName'] . "<br>";
echo "Password: " . $results['password'] . "<br>";
};
?>
白猪掌柜的
当年话下