实例
执行和测试文件
<?php require 'func_pdo.php'; //数据库连接 $type = 'mysql'; $host = '127.0.0.1'; $dbname = 'php'; $charset = 'utf8'; $user = 'root'; $pass = 'root'; $pdo = connect($dbname,$type,$host,$charset,$user,$pass); //新增操作 //$tabel = 'staff'; //$data = ['name'=>'林擒','sex'=>1,'age'=>18,'salay'=>20000]; //insert($pdo,$tabel,$data); //更新操作 //$tabel = 'staff'; //$data = ['name'=>'十三叔','sex'=>0,'age'=>28,'salay'=>25000]; //$where = 'staff_id=33'; //update($pdo,$tabel,$data,$where); //单条查询 //$table = 'staff'; //$fields=['name','age','salay']; //$fields = '*'; //$fields = 'name,salay'; //$where = 'age<40'; //echo '<pre>'.print_r(find($pdo,$table,$fields,$where),true); //多条查询 //$table = 'staff'; //$fields=['name','age','salay']; //$fields = '*'; //$fields = 'name,salay'; //$where = 'age<40'; //$order = 'age ASC'; //echo '<pre>'.print_r(select($pdo,$table,$fields,$where,$order),true); //删除 $table = 'staff'; $where = 'staff_id = 35'; delete($pdo,$table,$where);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php //数据库连接 if(!function_exists('connect')) { function connect($dbname,$type,$host,$charset,$user,$pass) { $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset}"; $username = 'root'; $password = 'root'; // $options = [ // PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //设置错误模式 // PDO::ATTR_CASE => PDO::CASE_NATURAL, //数据表字段保持不变 // PDO::ATTR_EMULATE_PREPARES => true, //启用PDO模拟 // PDO::ATTR_PERSISTENT => true, //启用持久性连接 // ]; try{ $pdo = new PDO($dsn,$username,$password); }catch (PDOException $e){ die('Connect ERROR!:'.$e->getMessage()); } return $pdo; } } //新增操作 if(!function_exists('insert')) { function insert($pdo,$table,$data=[]) { $sql = "INSERT IGNORE {$table} SET "; foreach (array_keys($data)as $filed){ $sql.= $filed.'=:'.$filed.','; } $sql = rtrim(trim($sql),',').';'; $stmt = $pdo->prepare($sql); foreach ($data as $filed=>$value){ $stmt->bindValue(":{$filed}",$value); } if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } }else{ return false; } } } //更新操作 if(!function_exists('update')){ function update($pdo,$table,$data=[],$where='') { $sql = "UPDATE {$table} SET "; foreach (array_keys($data)as$field){ $sql.=$field.'=:'.$field.','; } $sql = rtrim(trim($sql),','); if (!empty($where)){ $sql.=' WHERE '.$where; }else{ exit('条件不能为空'); } $stmt = $pdo->prepare($sql); foreach ($data as $field=>$value){ $stmt->bindValue(":{$field}",$value); } if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } }else { return false; } } } //查询操作 if(!function_exists('find')) { function find($pdo,$table,$fields,$where='') { $sql = " SELECT "; if(is_array($fields)){ foreach ($fields as $field){ $sql.=$field.','; } }else{ $sql .= $fields; } $sql.= ' FROM '.$table; if(!empty($where)){ $sql.=' WHERE '.$where; } $sql.=' LIMIT 1'; $sql = rtrim(trim($sql),',').';'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetch(); } }else{ return false; } } } //多条查询 if (!function_exists('select')) { function select($pdo,$table,$fields,$where,$order) { $sql = 'SELECT '; if(is_array($fields)){ foreach ($fields as $field){ $sql.=$field.','; } }else{ $sql.=$fields; } $sql.=' FROM '.$table; if(!empty($where)) { $sql .= ' WHERE '. $where; } if(!empty($order)) { $sql .= ' ORDER BY '. $order; } $sql = rtrim(trim($sql),',').';'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetchAll(); } }else{ return false; } } } if(!function_exists('delete')) { function delete($pdo, $table,$where='') { $sql = "DELETE FROM {$table} "; if(!empty($where)){ $sql .= ' WHERE '. $where; }else{ exit('条件不能为空'); } $sql = rtrim(trim($sql),',').';'; $stmt = $pdo->prepare($sql); //执行更新操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } } else { return false; } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例