Home > Backend Development > PHP Tutorial > 两种数据库读取方式,为什么会进入死循环?

两种数据库读取方式,为什么会进入死循环?

WBOY
Release: 2016-06-23 13:29:46
Original
1254 people have browsed it

正常写法:

<?$select=mysql_connect("localhost",'root','root') or die('hedapeng'.mysql_error());mysql_query('set names utf8');if(mysql_select_db('lishu',$select)){    echo 'hedapeng<br>';}else{    echo 'lishuwrong'.mysql_error().'<br>';}$b=mysql_query("select * from shoping") or die('lishudie'.mysql_error());//创建记录集$assoc=mysql_fetch_assoc($b);while ($assoc){    echo $assoc['item_id'].'----------'.$assoc['name'].'<br />';    $assoc=mysql_fetch_assoc($b);}mysql_close($select);?>
Copy after login

我改写后:
<?$select=mysql_connect("localhost",'root','root') or die('hedapeng'.mysql_error());mysql_query('set names utf8');if(mysql_select_db('lishu',$select)){    echo 'hedapeng<br>';}else{    echo 'lishuwrong'.mysql_error().'<br>';}//$b= or die('lishudie'.mysql_error());//创建记录集$assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));while ($assoc){    echo $assoc['item_id'].'----------'.$assoc['name'].'<br />';    $assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));}mysql_close($select);?>
Copy after login


回复讨论(解决方案)

$assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));
while ($assoc)
{
    echo $assoc['item_id'].'----------'.$assoc['name'].'
';
     $assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));
}
你每次都取查询结果的第一条记录
这个 $assoc 什么时候会变成逻辑假呢?

虽说编程可以有很大的灵活性,但基本的 定式是不可改变的

$assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));
while ($assoc)
{
    echo $assoc['item_id'].'----------'.$assoc['name'].'
';
     $assoc=mysql_fetch_assoc(mysql_query("select * from shoping"));
}
你每次都取查询结果的第一条记录
这个 $assoc 什么时候会变成逻辑假呢?

虽说编程可以有很大的灵活性,但基本的 定式是不可改变的


好像是有点懂了,刚开始学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