Php 在Smarty中显示所选记录
我从mysql中选择的查询是:Php 在Smarty中显示所选记录,php,database,arrays,smarty,Php,Database,Arrays,Smarty,我从mysql中选择的查询是: $query = mysql_query("SELECT * FROM news"); 然后我尝试使用mysql\u-fetch\u对象或mysql\u-fetch\u数组获取记录: $result = mysql_fetch_object($query); 然后我将此变量传递给Smarty: $smarty->assign('record',$result); 在smarty tpl文件中,我的代码是: <table> {foreach
$query = mysql_query("SELECT * FROM news");
然后我尝试使用mysql\u-fetch\u对象
或mysql\u-fetch\u数组
获取记录:
$result = mysql_fetch_object($query);
然后我将此变量传递给Smarty:
$smarty->assign('record',$result);
在smarty tpl文件中,我的代码是:
<table>
{foreach $record as $item}
<tr>
<td>{$item.title}</td>
<td>{$item.visit}</td>
<td>{$item.date}</td>
<td>{$item.date}</td>
<td>{$item.date}</td>
<td>{$item.date}</td>
<td>{$item.date}</td>
</tr>
{/foreach}
</table>
{foreach$记录为$item}
{$item.title}
{$item.visit}
{$item.date}
{$item.date}
{$item.date}
{$item.date}
{$item.date}
{/foreach}
但是在我运行代码之后,我会得到混乱的字符。
有什么问题吗
更新:
结果屏幕截图
看看这个
{foreach from=$record as $item}
<tr>
<td>{$item.title}</td>
<td>{$item.visit}</td>
<td>{$item.date}</td>
</tr>
{/foreach}
{foreach from=$record as$item}
{$item.title}
{$item.visit}
{$item.date}
{/foreach}
输出是什么,您的代码似乎是correct@m4t1t0样本编号或� 字符,我的表中有2条记录,但结果是我将得到大约12行!(在我的表格中,我有10列)如果您打印r($result);你得到了什么?当你使用mysql\u fetch\u数组而不是mysql\u fetch\u对象时,会发生同样的事情吗?你真的只需要做$result=mysql\u fetch\u对象($query)代码>?如果是,则只有一个对象,而不是对象数组。这可能会导致奇怪的输出您使用的是smarty 2.x语法,而他使用的是smarty 3.x语法。