鍍金池/ 問答/PHP/ mysqli_fetch_array 循環(huán)問題

mysqli_fetch_array 循環(huán)問題

<?php  
$q = isset($_POST["q"]) ? intval($_POST["q"]) : "";

if(empty($q)) {
    echo 'please choose a web site'; 
    exit;
}

$con = mysqli_connect('localhost','root','');

if(!$con) {
    die('Could not connect : '.mysqli_error($con));
}

//choose database
mysqli_select_db($con,'sixfive');

//set charset
mysqli_set_charset($con,'utf8');

$sql = "select * from websites where id='".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>SiteName</th>";
echo "<th>SiteURL</th>";
echo "<th>ALex</th>";
echo "<th>Country</th>";
echo "</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "</tr>";
}

echo "</table>";

mysqli_close($con);

?>

$q是接受Ajax傳過(guò)來(lái)的q的值

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "</tr>";
}

這個(gè)while中的 條件判斷不會(huì)死循環(huán)嗎? 運(yùn)行結(jié)果是沒有死循環(huán) $row = mysqli_fetch_array($result) 返回的值不是一直是true嗎?但是運(yùn)行結(jié)果正常 哪里的問題呢?

回答
編輯回答
入她眼

mysqli_fetch_array每獲取一條數(shù)據(jù)就將當(dāng)前記錄指向下一條,當(dāng)?shù)竭_(dá)記錄末尾時(shí),該函數(shù)返回false,退出while循環(huán)

2018年5月7日 15:04
編輯回答
愚念

mysqli_fetch_array這個(gè)函數(shù)是從結(jié)果集中取一行返回,如果沒有更多的行則返回false,所以不存在死循環(huán)這個(gè)操作的

2017年5月21日 11:05