PHP数据库(sql)查询在smarty tpl


PHP database (sql) query in smarty tpl

我是smarty的新手。我需要在我的.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 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"; 

require('../lib/SmartySQL.class.php');

$smarty = new SmartySQL(array('pdo_dsn' => 'mysql:dbname=db_name;host=localhost'),'pdo_username' => 'username','pdo_password' => 'password','pdo_driver_options' => array());

$ smarty ->显示("index.tpl");