简体   繁体   English

Smarty TPL中的PHP数据库(SQL)查询

[英]PHP database (sql) query in smarty tpl

Im a "noob" in smarty. 我是个聪明人。 I need to execute following code in one of my .tpl files: 我需要在我的.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';
}
?>

I've been trying for 3 hours now, surfing all over the web but without success. 我已经尝试了3个小时,在整个网上冲浪,但没有成功。 Help please! 请帮助!

您在列名中使用引号而不是反引号,只需将其更改以避免错误

SELECT `id` , `title` FROM `forum_posts` WHERE `bid` = '1' ORDER BY `date` DESC LIMIT 4"; 

require('../libs/SmartySQL.class.php'); 要求( '../库/ 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 =新的SmartySQL(array('pdo_dsn'=>'mysql:dbname = db_name; host = localhost','pdo_username'=>'用户名','pdo_password'=>'密码','pdo_driver_options'=> array( )));

$smarty->display('index.tpl'); $ smarty->显示器( '在index.tpl');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM