<?php
class pdo_con{
var $dsn = ‘mysql:dbname=test; host:127.0.0.1‘;
var $user = ‘root‘;
var $password = ‘‘;
var $opt = array(PDO::ATTR_PERSISTENT=>true);
var $dbh;
function __construct(){
try{
$this->dbh = new PDO($this->dsn,$this->user,$this->password,$this->opt); //面向对象 构造方法传值this
//var_dump($dbh);die();
}catch(PDOException $e){
echo ‘数据库连接失败:‘.$e->getMessage();
exit;
}
}
// $rs = $dbh->query(‘select * from user‘);
// var_dump($pdostatement->rowCount());die();
// var_dump($rs);
// echo "\nPDO是否关闭自动提交功能:".$dbh->getAttribute(PDO::ATTR_AUTOCOMMIT);
// echo "\n当前PDO的错误处理的模式:".$dbh->getAttribute(PDO::ATTR_ERRMODE);
// echo "\n表字段字符的大小写转换:".$dbh->getAttribute(PDO::ATTR_CASE);
// echo "\n与连接状态相关特有信息:".$dbh->getAttribute(PDO::ATTR_CONNECTION_STATUS);
// echo "\n空字符串转换为sql的NULL:".$dbh->getAttribute(PDO::ATTR_ORACLE_NULLS);
// echo "\n应用程序提前获取数据大小:".$dbh->getAttribute(PDO::ATTR_PERSISTENT);
// echo "\n数据库特有的服务器信息:".$dbh->getAttribute(PDO::ATTR_SERVER_INFO);
// echo "\n数据库服务器版本信息:".$dbh->getAttribute(PDO::ATTR_SERVER_VERSION);
// echo "\n数据库客户端版本号信息:".$dbh->getAttribute(PDO::ATTR_CLIENT_VERSION);
// echo "\n设置抛出异常处理错误:".$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
// echo "\n设置空字符串为null:".$dbh->setAttribute(PDO::ATTR_ORACLE_NULLS,true);
function select($table,$where=‘‘,$order=‘‘,$limits=‘‘,$field=‘‘){
$where = ($where==null)?‘‘:‘ WHERE ‘.$where;
$order = ($order==null)?‘‘:‘ ORDER BY ‘.$order;
$limits = ($limits==null)?‘‘:‘ limit ‘.$limits;
$field = ($field==null)?‘*‘:$field;
$sele = ‘SELECT ‘.$field.‘ FROM ‘.$table.$where.$order.$limits.‘;‘;
$kk=$this->dbh->query($sele);
$kk->setFetchMode(PDO::FETCH_ASSOC); //设置fetch在查询语句后面
return $kk;
}
}
$ss=new pdo_con();
// var_dump($ss);
$rs = $ss->select(‘user‘);
foreach ($rs as $value) {
# code...
var_dump($value);
// echo $value[‘username‘];
echo ‘<br>‘;
}