设为首页
收藏本站
开启辅助访问
切换到宽版
登录
立即注册
快捷导航
发布信息
搜索
搜索
网站首页
自丢网
社区论坛
自丢网论坛
附件中心
源码下载
邀请码
丢币充值
帖子
搜索附件
搜索附件
自丢网
›
附件中心
›
建站程序/IT教程
›
PHP教程
›
MyPDO.class.zip
板块导航
+
模板下载
+
建站程序/IT教程
·
源码下载
·
修罗xiuno教程
·
Discuz教程
·
zzz资源/教程
·
织梦教程
·
服务器教程
·
数据库教程
·
Html/Html5教程
·
JavaScript教程
·
Css/Css3教程
·
建站/web前端教程
·
PHP教程
·
其他编程教程
·
小程序app教程
·
PPT模板/素材
·
Word简历模板/素材
·
Excel模板/素材
·
素材中心
·
百科知识
+
综合教程资源
热门下载
左侧广告
版权所有:自丢网
For 2020-2050 ©
zidiu.com
MyPDO.class.zip
打开方式: winrar
资料大小: 1.35 KB
下载次数: 568
上传会员:
admin
上传时间: 2020-11-16
本站网址:
www.zidiu.com
查看附件所在的主题
下载附件
复制链接推荐给好友
点击附件下载:下载附件
PDO数据库初始化连接数据库文件代码(附源码下载):
PDO数据库初始化文件代码(附源码下载)
<?php
class MyPDO{
private $type; //数据库类别
private $host; //主机地址
private $port; //端口号
private $dbname; //数据库名
private $charset; //字符集
private $user; //用户名
private $pwd; //密码
private $pdo; //保存PDO对象
private static $instance;
private function __construct($param) {
$this->initParam($param);
$this->initPDO();
$this->initException();
}
private function __clone() {
}
public static function getInstance($param=array()){
if(!self::$instance instanceof self)
self::$instance=new self($param);
return self::$instance;
}
//初始化参数
private function initParam($param){
$this->type=$param['type']??'mysql';
$this->host=$param['host']??'127.0.0.1';
$this->port=$param['port']??'3306';
$this->dbname=$param['dbname']??'data';
$this->charset=$param['charset']??'utf8';
$this->user=$param['user']??'root';
$this->pwd=$param['pwd']??'root';
}
//初始化PDO
private function initPDO(){
try{
$dsn="{$this->type}:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}";
$this->pdo=new PDO($dsn, $this->user, $this->pwd);
} catch (PDOException $ex) {
$this->showException($ex);
exit;
}
}
//显示异常
private function showException($ex,$sql=''){
if($sql!=''){
echo 'SQL语句执行失败<br>';
echo '错误的SQL语句是:'.$sql,'<br>';
}
echo '错误编号:'.$ex->getCode(),'<br>';
echo '错误行号:'.$ex->getLine(),'<br>';
echo '错误文件:'.$ex->getFile(),'<br>';
echo '错误信息:'.$ex->getMessage(),'<br>';
}
//设置异常模式
private function initException(){
$this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
//执行增、删、改操作
public function exec($sql){
try{
return $this->pdo->exec($sql);
} catch (PDOException $ex) {
$this->showException($ex, $sql);
exit;
}
}
//获取自动增长的编号
public function lastInsertId(){
return $this->pdo->lastInsertId();
}
//判断匹配的类型
private function fetchType($type){
switch ($type){
case 'num':
return PDO::FETCH_NUM;
case 'both':
return PDO::FETCH_BOTH;
case 'obj':
return PDO::FETCH_OBJ;
default:
return PDO::FETCH_ASSOC;
}
}
//获取所有数据 ,返回二维数组
public function fetchAll($sql,$type='assoc'){
try{
$stmt=$this->pdo->query($sql); //获取PDOStatement对象
$type= $this->fetchType($type); //获取匹配方法
return $stmt->fetchAll($type);
} catch (Exception $ex) {
$this->showException($ex, $sql);
}
}
//获取一维数组
public function fetchRow($sql,$type='assoc'){
try{
$stmt=$this->pdo->query($sql); //获取PDOStatement对象
$type= $this->fetchType($type); //获取匹配方法
return $stmt->fetch($type);
} catch (Exception $ex) {
$this->showException($ex, $sql);
exit;
}
}
//返回一行一列
public function fetchColumn($sql){
try{
$stmt=$this->pdo->query($sql);
return $stmt->fetchColumn();
} catch (Exception $ex) {
$this->showException($ex, $sql);
exit;
}
}
}
复制代码
源码下载: