1.数据库链接封装
实例
//1.数据库链接封装 function connect($url,$dbname,$root,$pass,$charset='utf8',$port='3306',$type='mysql'){ $dsn="{$type}:host={$url};dbname={$dbname};charset={$charset};port={$port}"; $userName = $root; $passWord = $pass; try{ $pdo = new PDO($dsn,$userName,$passWord); // echo '数据库链接成功!'; return $pdo; }catch (PDOException $e){ print 'Connect ERROR!:'.$e->getMessage(); die(); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
函数调用>>>
实例
require 'lib/pdo_connect.php'; $pdo = connect('127.0.0.1','php','root','root');
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.数据插入操作
实例
//2.单条数据插入操作 //insert ignoe table set __=__ function insert_one($pdo,$table,$data=[]) { $sql = "insert ignore {$table} set "; if(!empty($data)){ //1.SQL语句拼接 //2.将data[]数组中的key作为参数以PDO预处理方式进行拼接 foreach (array_keys($data) as $filed){ $sql .= " {$filed} =:{$filed},"; } //3.去除最后一个逗号 $sql = substr($sql,0,strlen($sql)-1).';'; $stmt = $pdo->prepare( $sql); foreach ($data as $key => $value) { $stmt->bindValue(":{$key}",$value); } //echo $stmt->queryString; //echo is_array($data); if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } } else { echo 'STMT Error!:'.$pdo->errorInfo(); return false; } }else{ echo '无要新增的数据'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
函数调用
实例
//1.新增数据 $data = ['name'=>'虞姬','age'=>24,'address'=>'江东','iphone'=>'88889999']; insert_one($pdo,'staff',$data);
运行实例 »
点击 "运行实例" 按钮查看在线实例
3.更新操作
实例
//3.更新操作 //update table set ?=? where function update($pdo,$table,$data=[],$where){ $sql = "update {$table} set "; foreach (array_keys($data) as $key){ $sql .= " {$key}=:{$key},"; } $sql = substr($sql,0,strlen($sql)-1); if(!empty($where)){ $sql .= " where {$where} ".';'; } $stmt = $pdo->prepare($sql); foreach ($data as $key=>$value){ $stmt->bindValue(":{$key}",$value); } // echo $stmt->queryString; if($stmt->execute()){ echo '更新成功'; return true; }else{ echo '更新失败'; return false; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
更新调用
实例
//2.更新数据 //$data1 = ['name'=>'刘邦','age'=>28]; //update($pdo,'staff',$data1,'staff_id=13');
运行实例 »
点击 "运行实例" 按钮查看在线实例
4.删除操作
实例
//4.删除操作 function delete($pdo,$table,$data=[]){ $sql = "delete from {$table} where "; if(!empty($data)){ if( count($data)==1 ){ foreach (array_keys($data) as $key){ $sql .= "{$key}=:$key ;"; } }else{ foreach (array_keys($data) as $key){ $sql .= "{$key}=:$key and "; } $sql = substr($sql,0,strripos($sql,'and')-1); } }else{ echo '查询条件不能为空'; die(); } $stmt = $pdo->prepare($sql); foreach ($data as $key=>$value){ $stmt->bindValue(":{$key}",$value); } //echo $stmt->queryString; if($stmt->execute()){ if($stmt->rowCount()>0) { echo '<br>删除成功'; return true; }else{ echo '<br>删除失败'; return false; } }else{ echo '<br>删除失败'; return false; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
删除调用
实例
//3.删除数据 //$data2 = ['name'=>'虞姬','age'=>24,'address'=>'江东','iphone'=>'88889999']; //delete($pdo,'staff',$data2);
运行实例 »
点击 "运行实例" 按钮查看在线实例
5.查询
实例
//4.精准条件查询 function select($pdo,$col=[],$table,$data=[]){ $sql = "select "; if(!empty($col)) { if(count($col)==1){ foreach ($col as $value){ $sql .= " {$value} "; } }else{ foreach ($col as $value){ $sql .= " {$value} ,"; } $sql=substr($sql,0,strlen($sql)-1); } }else{ echo '查询字段不能为空'; die(); } $sql .= " from {$table} where 1=1 "; if(!empty($data)) { foreach (array_keys($data) as $key){ $sql .= " and {$key}=:{$key} "; } }else{ $sql .= " and 1=1 "; // echo '查询条件不能为空'; // die(); } $stmt = $pdo->prepare($sql); foreach ($data as $key=>$value){ $stmt->bindValue(":{$key}",$value); } // echo $sql; // die(); if($stmt->execute()){ $resall=[]; while($res = $stmt->fetch(PDO::FETCH_ASSOC)){ $resall[]=$res; //var_dump($res); } return $resall; }else{ echo '<br>查询失败'; return false; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
查询调用
实例
//4.精准条件查询 //4.1 模糊查询全部 $data2 = ['*']; $data = []; //4.1 指定字段查询 //$data2 = ['name','age','address']; //$data = ['age'=>24]; $res = select($pdo,$data2,'staff',$data); foreach($res as $key => $link) { foreach($link as $key1 => $val) { echo $key1 . '-->'.$val." "; } echo '<br>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例