smarty模板循环读取数据库内容问题,只能读取第一条,为什么?

我的数据库字段有:id,title,author,datePHP代码如下:<?phprequire'smarty.inc.php';//引入smartyrequire'c... 我的数据库字段有:id,title,author,date
PHP代码如下:
<?php
require 'smarty.inc.php';//引入smarty
require 'configs/mysql_db.php'; //引入配置文件
global $_smarty;
$sql="select * from news";
$res=mysql_query($sql);
$_row=mysql_fetch_assoc($res);
$_smarty->assign('row',$_row);
$_smarty->display('index.html');
?>

HTML代码如下:
<body>
<br />
<hr>
{<foreach from=$row item=a>}
{<$a>}<br />
{</foreach>}
</body>
运行后却只能读取第一条,而且我只能是想读取标题和时间,它却把第一条全部读取出来了。请大侠帮忙解决。
展开
 我来答
阳光的豆豆鱼
推荐于2016-08-30 · TA获得超过256个赞
知道小有建树答主
回答量:483
采纳率:76%
帮助的人:188万
展开全部
<?php
require 'smarty.inc.php';//引入smarty
require 'configs/mysql_db.php'; //引入配置文件
global $_smarty;
$sql="select * from news";
$res=mysql_query($sql);
$_row=array();
while(r =mysql_fetch_assoc($res)){
$_row[]= r

}
$_smarty->assign('row',$_row);
$_smarty->display('index.html');
?>

注意mysql_fetch_assoc只能取出一条记录

HTML:
<body>
<br />
<hr>
假如time表示时间字段,title表示标题字段,具体根据你自己的字段来
{<foreach from=$row item=a>}
{<$a.time>}<br />
{<$a.title>}<br />
{</foreach>}
</body>
追问
多谢!!!!  原来先要定义一个空数组
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式