2015-02-11 58 views
0

我試圖從一個mysql查詢設置值到模板中的小鬍子。 我使用mustache.php可以這樣做嗎?我無法找到有關模板查詢的任何文檔。有沒有人有更好的解決方案?有沒有辦法在小鬍子模板中包含mysql查詢?

<?php 
/* 
* PHP Mustache - Basic Mustache example in PHP 
*/ 
$servername = "localhost"; 
$database = "laravel"; 
$username = "root"; 
$password = "root"; 

try { 

    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception 

} catch(PDOException $e) { 
    echo "Connection failed: " . $e->getMessage(); 
} 

$query = $conn->query('SELECT * FROM users'); 

//register mustache library 
require 'Mustache/Autoloader.php'; 
Mustache_Autoloader::register(); 


//set the template string (obviously complex application will use files) 
$template = 'Hello, {{name}},<br /> Your last name is {{lastname}}'; 


//set the template values 
$values = array(
    $query = $conn->query('SELECT * FROM users'); 
    'name' = $row['name']; 
    'lastname' = $row['lastname']; 
); 


//start the mustache engine 
$m = new Mustache_Engine; 

//render the template with the set values 
echo $row['name']; 
echo $row['lastname']; 
echo $m->render($template, $values); 
?> 
+0

有與此代碼這麼多的錯誤,我不知道從哪裏開始。這不是創建數組的有效方法! $行從未被分配。 – 2015-02-11 12:11:45

+0

@MarkusMüller我整理了其餘的代碼。你能告訴我我要去哪裏嗎?我是新來的PHP – user2003341 2015-02-11 12:17:20

回答

0

嘗試像這樣

$statement = $conn->query('SELECT * FROM users'); 
$row = $statement->fetch(PDO::FETCH_ASSOC); 

//start the mustache engine 
$m = new Mustache_Engine; 
echo $m->render($template, $row); 
相關問題