我有我的数据库类设置
<?php
class Database
{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct()
{
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO Instance
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
?>
我的模板类
<?php
/*
* Template Class
* Creates a template/view object
*/
class Template
{
// Path to template
protected $template;
// Variables passed in
protected $vars = array();
/*
* Class Constructor
*/
public function __construct($template)
{
$this->template = $template;
}
/*
* Get Template Variables
*/
public function __get($key)
{
return $this->vars[$key];
}
/*
* Set Template Variables
*/
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
/*
* Convert Object To String
*/
public function __toString()
{
extract($this->vars);
chdir(dirname($this->template));
ob_start();
include basename($this->template);
return ob_get_clean();
}
}
?>
主题课
<?php
class Topic
{
// Init DB Variable
private $db;
/*
* Constructor
*/
public function __construct()
{
$this->db = new Database();
}
/*
* Get All Topics
*/
public function getAllTopics()
{
$this->db->query("SELECT topics.*, users.username, users.avatar, categories.name
FROM topics
INNER JOIN users
ON topics.user_id = users.id
INNER JOIN categories
ON topics.category_id = categories.id
ORDER BY create_date DESC");
// Assign Result Set
$results = $this->db->resultset();
return $results;
}
}
然后我的索引控制器NB:我将数据库链接到(init.php),以及使用函数__autoload($ className){require_once(&#39; libraries /&#39;。$ className)自动加载所有类。 &#39; .PHP&#39);}
<?php require('core/init.php'); ?>
<?php
// Create the Topic Object
$topic = new Topic;
// Get Template & Assign Variables
$template = new Template('templates/frontpage.php');
// Assign Variables
$template->topics = $topic->getAllTopics();
// Display Template
echo $template;
在frontpage.php上,我尝试访问$ topics类的属性,它返回ERROR - 注意:尝试在C:\ wamp \ www \ talkingspace \ templates \ frontpage中获取非对象的属性。 PHP
以下是我的样本首页
<?php foreach ($topics as $topic) { ?>
<div class="topic-content">
<h3><a href="topic.php"><?php echo $topic->title; ?></a></h3>
<div class="topic-info">
<a href="topics.php?category=<?php echo $topic->category_id; ?>"><?php echo $topic->name; ?></a> >>
<a href="topics.php?user=<?php echo $topic->user_id; ?>"><?php echo $topic->username; ?></a> >> Posted on <?php echo $topic->create_date; ?>
<span class="badge pull-right"><?php echo $topic->id; ?></span</div></div>
<?php } ?>
请有人能告诉我什么是错的吗?我将不胜感激,谢谢!
答案 0 :(得分:0)
您可以从此行获得结果:
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
这将返回一个数组。
在您使用$topic->someField
如果您希望这样做,那么您有两个选择:
PDO::FETCH_OBJ
$topic['somefield']