Alen
Alen

Reputation: 21

PHP database (sql) query in smarty tpl

Im a "noob" in smarty. I need to execute following code in one of my .tpl files:

  <? // 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. Help please!

Upvotes: 2

Views: 4836

Answers (2)

Shambhulal Verma
Shambhulal Verma

Reputation: 67

require('../libs/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->display('index.tpl');

Upvotes: 0

Fabio
Fabio

Reputation: 23500

You are using quotes instead of backtick for column name, simply change them to avoid the error

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

Upvotes: 1

Related Questions