Home > Backend Development > PHP Tutorial > 这个语句循环了几次

这个语句循环了几次

WBOY
Release: 2016-06-06 20:22:42
Original
1094 people have browsed it

<code>$sql = "select id,username,mail from admin;";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)){
    $rows[]=$row;
}</code>
Copy after login
Copy after login

这个while循环了几次? 是看数据库中的结果么? 这里$row=mysql_fetch_array($result)是赋值语句把。应该一直为真啊

回复内容:

<code>$sql = "select id,username,mail from admin;";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)){
    $rows[]=$row;
}</code>
Copy after login
Copy after login

这个while循环了几次? 是看数据库中的结果么? 这里$row=mysql_fetch_array($result)是赋值语句把。应该一直为真啊

自行 echo mysql_num_rows($results)看有几行,

很多人从来不判断是否查询有结果就去循环了,这个习惯不好。

mysql_num_rows($result)可以看到结果集中有多少行记录,就知道while循环的次数。

取出来多少条数据,就循环多少次
每执行一次mysql_fetch_array($result)
赋值给$row
会递减1
直到为false

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template