【发布时间】:2014-05-29 18:04:12
【问题描述】:
您好,每次我在 fetch() 函数上使用 while 循环时,我都在尝试使用用 PHP 编写的准备好的语句来遍历我的数据库表中的所有行 它让我陷入无限循环这是我的代码,请帮助
public function loadIndex() {
$conn = new Database();
$db = $conn->Connect();
$query = "
SELECT Title,Description,uploadeddate
FROM article
ORDER BY article.uploadeddate DESC LIMIT 10";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($title, $desc, $date);
$num_rows = $stmt->num_rows();
$fetch = $stmt->fetch();
$data = array(
'title' => $title,
'desc' => $desc,
'date' => $date,
'num_rows' => $num_rows,
'fetch' => $fetch
);
return $data;
}
这就是用途
<?php
art = new Articles();
$index = $art->loadIndex();
$num = $index['num_rows'];
if($num != 0) {
while() {$index['fetch']} {
echo $index['title']."<br />";
echo $index['desc']."<br />";
echo $index['date']."<br />";
}
}?>
谢谢
【问题讨论】:
-
附注:不要在每个函数中连接。你会杀死你的mysql服务器。你应该只连接一次
-
看来死循环是因为你的while语句没有条件
-
就您的代码而言,您需要在函数内部而不是外部运行 while 循环
标签: php while-loop