我正在创建一个OO CRUD系统,但我没有看到为什么我没有收到任何行。连接工作,数据在表中,当我将表名硬编码到查询中时,它可以工作。但是,当使用占位符时,它不起作用。非常感谢任何帮助。
的index.php
<html>
<head>
<title>Content Management System</title>
<?php include'class.DB.php'; ?>
</head>
<body>
<table style="width:100%">
<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Email</td></tr>
<tr></tr>
<tr></tr>
</table>
<?php
$customer = new DB();
$table = $customer->getTable('customer');
foreach($table as $row){
echo $row->id;
}
?>
</body>
</html>
class.DB.php
包括&#39; class.config.php&#39;;
class DB {
private $db;
function __construct(){
$this->db = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'hidden', 'hidden');
if($this->db){
return $this->db;
}
echo 'error';
}
function getTable($name){
$stmt = $this->db->prepare('SELECT * FROM :table');
$stmt->bindParam(':table',$name,PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll();
echo $stmt->rowCount();
return $results;
}
}