mysql_fetch_array()做无限循环

时间:2012-10-14 16:05:28

标签: php mysql loops

我不知道为什么,一个应该是有限的循环,比如

while ($noticia = mysql_fetch_array(mysql_query("SELECT * FROM noticias WHERE deletada='0' ORDER BY id ASC")))
{
    // echo $noticia['blabla'];
}

正在进行无限循环,页面无法加载......

为什么会这样?

2 个答案:

答案 0 :(得分:4)

可以通过

来解决
  $qry = mysql_query("SELECT * FROM noticias WHERE deletada='0' ORDER BY id ASC")

  while ($noticia = mysql_fetch_array($qry ))
    {
        // echo $noticia['blabla'];
    }

但不推荐使用mysql_*函数,因此请使用pdo或mysqli

pdo是一个简单的简单here is very simple pdo tutorial

$pdo = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$query = $pdo->prepare("SELECT * FROM noticias WHERE deletada='0' ORDER BY id ASC");
$query->execute();
$row=$articlequery->fetch(pdo::FETCH_ASSOC);

答案 1 :(得分:1)

试试这个。

$query = mysql_query("SELECT * FROM noticias WHERE deletada='0' ORDER BY id ASC");
while($noticia = mysql_fetch_array($query)){
    echo $noticia['table_field'];
}