自己写的一个基本的数据库操作类,本来以为应该轻车熟路的,结果发现了很多问题,突然想起了一句话:很多事情你知道怎么做和你会做之间是有很大差异的;所有搞技术的童鞋,应该以我未戒,不要在觉得会上犯了错误,哈哈!
总结哈:
1.以后就把CSDN当成个人的一个技术日志了,没事发发动态玩,顺便写写生活!
2.突然觉得搞技术的很多同事说话好直接,还是要学会控制脾气,不轻易对别人发脾气,待人和善,学会和人打交道,不做技术宅男,不死钻技术!
3.每天要早起啊,这段时间放纵自己了,今天起个早床,感慨万千:每天早起一个小时,一年你就比别人多活了半个月,半个月的时间你可能又碰到了很多别人想不到的机会,差距就是这么产生的,居安思危!
4.听我废话这么多,看看我写的哈,菜鸟一枚,最好是私聊我给点建议,哈哈!说不定我们就成了朋友了。我在深圳,你在哪?
<?php
class Mysql{
private $conn; //数据库连接资源
private $error; //错误信息
private $dbtype; //所连接数据库类型
private $host; //主机名
private $dbname; //数据库名
private $port; //连接端口号
private $user; //连接用户名
private $pwd; //密码
private $charset; //设置连接所采用的字符集
function __construct(){
//载入配置文件
include './include/config.php';
$this->dbtype=$cfg['dbtype'];
$this->host=$cfg['host'];
$this->dbname=$cfg['dbname'];
$this->port=$cfg['port'];
$this->user=$cfg['user'];
$this->pwd=$cfg['pwd'];
$this->charset=$cfg['charset'];
try{
$conn=new PDO("$this->dbtype:host=$this->host;port=$this->port;dbname=$this->dbname",$this->user,$this->pwd);
$this->conn=$conn;
}catch(PDOException $e){
$this->error="数据库连接不成功!".$e->getMessage()."<br>";
}
$this->conn->query("set names $this->charset");
}
//查询所有行数据
public function getAll($sql){
$stmt=$this->conn->prepare($sql);
$result=$stmt->execute();
if($result){
$lists=$stmt->fetchAll(PDO::FETCH_ASSOC);
return $lists;
}else{
return false;
}
}
//删除方法
public function delete($sql){
$stmt=$this->conn->prepare($sql);
$result=$stmt->execute();
//用受影响的行数来作为判断是否执行成功的条件
if($stmt->rowCount()){
return true;
}else{
return false;
}
}
//获取单行数据
public function getRow($sql){
$stmt=$this->conn->prepare($sql);
$result=$stmt->execute();
if($result){
return $info=$stmt->fetch(PDO::FETCH_ASSOC);
}else{
return false;
}
}
//更新数据
public function update($sql){
$stmt=$this->conn->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
return true;
}else{
return false;
}
}
//插入一行数据
public function add($sql){
$stmt=$this->conn->prepare($sql);
$result=$stmt->execute();
if($stmt->rowCount()){
return $result;
}else{
return false;
}
}
//获取错误信息
public function getError(){
return $this->error();
}
}
?>
原文链接:https://blog.csdn.net/living_ren/article/details/78777225
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~