<?php
class QueryAction
{
var $dbh;
function __construct()
{
$this->conn();
}
function conn()
{
$this->dbh=new PDO('mysql:host=localhost;dbname=db_name',"root","root");
$this->dbh->query("set names utf8");
}
function find()
{
$stmt=$this->dbh->prepare("select * from table_name");
$stmt->execute();
$data=$stmt->fetchAll();
return $data;
}
function close()
{
$this->dbh=null;
}
}
?>
<html>
<table>
<?php
$query=new QueryAction();
$data=$query->find();
foreach ($data as $cols){//$col为别名
?>
<tr>
<td><?php echo $cols['col'];?></td>//单引号中的col为数据表中的列名
</tr>
<?php }?>
</table>
</html>
参考书籍:辛洪郁 张彦国 邹淑芳.PHP程序开发范例宝典[M].北京:人民邮电出版社