PHP函数库的封装
<?php //链接数据库 if (!function_exists('connect')) { /** * @param $dbname * @param string $type * @param string $host * @param string $charset * @param int $port */ function connect($dbname,$type='mysql',$host='127.0.0.0',$charset='utf8',$port=3306,$user='root',$pass='root'){ $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port}"; $userName = $user; $password = $pass; try{ $pdo = new PDO($dsn,$userName,$password); echo '连接成功'; }catch (PDOException $e){ print '连接错误'.$e->getMessage(); die(); } return $pdo; } } //新增数据 if (!function_exists('insert')){ /** * @param $pdo * @param $table * @param array $data */ function insert($pdo,$table,$data=[]){ $sql = "INSERT IGNORE {$table} SET "; foreach (array_keys($data) as $field){ $sql .= $field . ' =:' . $field . ','; } $sql = rtrim(trim($sql), ',').';'; $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('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('条件不能为空'); } $sql = rtrim(trim($sql), ',').';'; // die($sql); $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')){ /** * @param $pdo * @param $table * @param $fields * @param string $where * @return bool */ function find($pdo,$table,$fields,$where=''){ $sql = "SELECT "; if (is_array($fields)){ foreach ($fields as $field){ $sql .= $field . ','; } }else{ $sql .= $fields.','; } $sql = rtrim(trim($sql), ','); $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 = rtrim(trim($sql), ','); $sql .= ' FROM '.$table; if (!empty($where)){ $sql .= ' WHERE '.$where; } if (!empty($order)){ $sql .= ' ORDER BY '.$order; } $sql = rtrim(trim($sql), ',').';'; // die($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} "; $sql = rtrim(trim($sql), ','); if (!empty($where)){ $sql .= ' WHERE '.$where ; }else{ exit('条件不能为空'); } $sql = rtrim(trim($sql), ',').';'; // die($sql); $stmt = $pdo->prepare($sql); if ($stmt->execute()){ if ($stmt->rowCount()>0){ return true; } }else{ return false; } } }
执行语句
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/2 * Time: 15:13 */ require 'lib/func_pdo.php'; $type = 'mysql'; $host = '127.0.0.1'; $dbname = 'php'; $charset = 'utf8'; $port = '3306'; $user = 'root'; $pass = 'root'; $pdo = connect($dbname, $type, $host, $charset, $port, $user, $pass); //2新增测试 //$table = 'staff'; //$data = ['name'=>'小王','sex'=>0,'age'=>32, 'salary' => 8800]; //insert($pdo, $table, $data); //更新操作 //$table = 'staff'; //$data = ['name'=>'西门','sex'=>0,'age'=>42, 'salary' => 8990]; //$where = 'staff_id=26'; //update($pdo, $table, $data,$where); //单条查询 //$table = 'staff'; //$fields = ['name','age','salary']; //$fields = '*'; //$fields = 'name,salary'; //$where = 'age < 30 '; //echo print_r(find($pdo, $table, $fields, $where),true); //多语句查询 //$table = 'staff'; //$fields = ['name','age','salary']; //$fields = '*'; //$fields = 'name,salary'; //$where = 'age < 26 '; //$order = 'age ASC'; //echo '<pre>'. print_r(select($pdo, $table, $fields, $where,$order),true).'</pre>'; // 删除 $table = 'staff'; $where = 'staff_id = 21'; delete($pdo, $table, $where);
总结,使用PDO 进行函数库的封装,以后使用起来更方便
首先判断函数是否存在使用function_exists()
调用connect()函数执行连接数据库,代码测试使用die()看看是否执行成功,或者到哪一步;