smarty tpl中的PHP数据库(sql)查询
我是smarty的“noob”。我需要在一个.tpl文件中执行以下代码:smarty tpl中的PHP数据库(sql)查询,php,sql,smarty,Php,Sql,Smarty,我是smarty的“noob”。我需要在一个.tpl文件中执行以下代码: <? // SELECT sql query $sql = "SELECT 'id' , 'title' FROM `forum_posts` WHERE bid = '1' ORDER BY 'date' DESC LIMIT 4"; // perform the query and store the result $result = query($sql); // if the $result con
<? // SELECT sql query
$sql = "SELECT 'id' , 'title' FROM `forum_posts` WHERE bid = '1' ORDER BY 'date' DESC LIMIT 4";
// perform the query and store the result
$result = query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo '<tr>
<td><a href="http://www.site.com/forum.php?topic='. $row['id']. '">'. $row['title']. '</a> </td>
</tr> ';
}
}
else {
echo 'No news';
}
?>
我已经试了3个小时了,在网上冲浪,但是没有成功。
救命啊 如果使用引号而不是反勾选列名,只需更改引号即可避免错误
SELECT `id` , `title` FROM `forum_posts` WHERE `bid` = '1' ORDER BY `date` DESC LIMIT 4";
需要('../libs/SmartySQL.class.php')
$smarty=new-SmartySQL(数组('pdo_dsn'=>'mysql:dbname=db_name;host=localhost',
“pdo_用户名”=>“用户名”,
“pdo_密码”=>“密码”,
“pdo_驱动程序_选项”=>array())
$smarty->display('index.tpl') 你的模板为什么要执行查询?你处理得不对。不要在模板文件中执行查询。在调用模板的PHP文件中执行它,然后
$smarty->assign()
获取到模板中使用的数组的行。我已经创建了一个文件news.PHP,并将其包含到一个tpl文件中:{include file='newsacc.PHP'}
,但我猜这是错误的。我没有任何“聪明”的想法。我有一个使用smarty的脚本,只想添加这个小部件。关于你所说的有什么好的例子吗?谢谢,你不应该只回答一段代码。欢迎稍作解释。