Correction status:qualified
Teacher's comments:
自己敲遍学习
<?php require 'lib/func_pdo1.php'; //1.连接测试 $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); //insert $table = 'staff'; $data = ['name'=>'zhuang','sex'=>1,'age'=>40,'salary'=>6000]; //insert($pdo,$table,$data); //update $table = 'staff'; $where='staff_id=21'; //查询条件使用字符串直接传入 $data = ['name'=>'php','sex'=>0, 'age'=>30,'salary'=>500]; update($pdo,$table,$data,$where); //4.单条查询测试 $table = 'staff'; $fields = ['name','age','salary']; //$fields = '*'; $fields = 'name,salary'; $where = 'age > 40'; //echo '<pre>'.print_r(find($pdo, $table, $fields, $where),true).'</pre>'; //5.多条查询测试 $table = 'staff'; $fields = ['name','age','salary']; $fields = '*'; $where = 'age < 40'; $order = 'name asc'; echo '<pre>'.print_r(select($pdo, $table, $fields, $where, $order),true).'</pre>'; //6.删除测试 $table = 'staff'; $where = 'staff_id = 25'; $where = ''; //delete($pdo, $table, $where);
点击 "运行实例" 按钮查看在线实例
func_pdo1.php
<?php if(!function_exists('connect')) { /** * @param $dbname * @param $type * @param string $host * @param string $charset * @param int $port * @param string $user * @param string $pass * @return PDO */ function connect( $dbname,$type='mysql',$host='127.0.0.1', $charset='utf8', $port=3306,$user='root',$pass='root') { $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port} "; $userName = 'root'; $password = 'root'; $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_PERSISTENT => true, ]; try { $pdo = new PDO($dsn, $userName, $password, $options); } catch (PDOException $e) { die('Connect ERROR!:' . $e->getMessage()); } return $pdo; } } //insert if(!function_exists('insert')) { 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; } } } //update 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')) { /** * 查询单条记录 * @param $pdo * @param $table * @param $fields * @param string $where * @return bool */ function find($pdo,$table,$fields, $where='') { //创建SQL语句 $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),',').';'; //创建PDO预处理对象 $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')) { /** * 查询多条记录 * @param $pdo * @param $table * @param $fields * @param string $where * @return bool */ function select($pdo,$table,$fields, $where='', $order='') { //创建SQL语句 $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),',').';'; //创建PDO预处理对象 $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')) { /** * 删除记录 * @param $pdo * @param $table * @param string $where * @return bool */ function delete($pdo,$table, $where='') { //创建SQL语句 $sql = "DELETE FROM {$table} "; //添加删除条件 if(!empty($where)) { $sql .= 'WHERE '. $where; }else{ exit('条件不能为空'); } //去掉尾部逗号,并添加分号结束 $sql = rtrim(trim($sql),',').';'; //创建PDO预处理对象 $stmt = $pdo->prepare($sql); //执行删除操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } } else { return false; } } }
点击 "运行实例" 按钮查看在线实例