Correction status:Uncorrected
Teacher's comments:
<?php //连接数据库 if(!function_exists('connect')){ /** * [mysql数据库连接] * @param [type] $dbname [description] * @param string $type [description] * @param string $host [description] * @param string $charset [description] * @param integer $port [description] * @param string $user [description] * @param string $pass [description] * @return [type] [description] */ 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=$user; $password=$pass; $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类,创建PDO对象 $pdo = new PDO($dsn, $username, $password, $options);echo '数据库连接成功(请删除)<br>'; } catch (PDOException $e) { die('Connect ERROR!:'.$e->getMessage()); } return $pdo; }} //插入数据 if(!function_exists('insert')){ /** * 增加数据 * @param [type] $pdo [description] * @param [type] $table [description] * @param array $data [description] * @return [type] [description] */ function insert($pdo,$table,$data=[]) { //创建sql 语句 $sql="INSERT IGNORE {$table} SET ";//数据库 键值要设为唯一ignore 才有效果 //提取数据中的键,放入sql foreach (array_keys($data) as $field) { $sql .=$field.' =:'.$field.','; } //去除“,”号,加上“;”号 $sql=rtrim(trim($sql),',').';';//die($sql);//检测sql语句 $yu=$pdo->prepare($sql); //绑定 foreach ($data as $key => $value) { $yu->bindValue(":{$key}",$value); } //执行 if($yu->execute()){ if($yu->rowCount()>0){ return true; } }else{ return false; } }} //删除数据 if(!function_exists('delete')){ /** * [delete description] * @param [type] $pdo [description] * @param [type] $table [description] * @param string $where [description] * @return [type] [description] */ function delete($pdo,$table,$where=''){ $sql="DELETE FROM {$table} "; if (!empty($where)) { $sql .='WHERE '.$where.';'; }else{ exit('删除数据,条件不能为空'); } //die($sql); $yu=$pdo->prepare($sql); if($yu->execute()){ if($yu->rowCount()>0){ echo '删除成功'; return true; } }else{ echo '删除失败'; return false; exit(); } }} //修改数据 if (!function_exists('update')) { function update($pdo,$table,$data=[],$where=''){ $sql="UPDATE {$table} SET "; foreach (array_keys($data) as $key) { $sql.=$key.'=:'.$key.','; } $sql=rtrim(trim($sql),','); if (!empty($where)) { $sql .=' WHERE '.$where.';'; }else{ exit('修改数据,条件不能为空'); } // die($sql); $yu=$pdo->prepare($sql); foreach ($data as $key => $value) { $yu->bindValue(":{$key}",$value); } //die($sql); if($yu->execute()){ if($yu->rowCount()>0){ echo '修改成功'; return true; } }else{ echo '修改失败'; return false; }}} //单条语句查询 if(!function_exists('one_select')){ /** * [one_select description] * @param [type] $pdo [description] * @param [type] $table [description] * @param [type] $fields [description] * @param string $where [description] * @return [type] [description] */ function one_select($pdo,$table,$fields,$where=''){ $sql="SELECT "; if(!empty($fields)){ if(is_array($fields)){ foreach ($fields as $key ) { $sql.=$key.','; } }else{ $sql.=$fields; } }else{ echo '没有查询字段'; exit(); } $sql=rtrim(trim($sql),','); $sql.=' FROM '.$table; if(!empty($where)) { $sql .= ' WHERE '. $where.';'; }else{ $sql .=' LIMIT 1;';//没有条件,就显示第一条记录 } //die($sql); $yu=$pdo->prepare($sql); if($yu->execute()){ if($yu->rowCount()>0){ $yu->setFetchMode(PDO::FETCH_ASSOC); return $yu->fetch(); } }else{ return false; } } } //多条语句查询 if(!function_exists('duo_select')){ /** * [duo_select description] * @param [type] $pdo [description] * @param [type] $table [description] * @param [type] $fields [description] * @param string $where [description] * @param string $order [description] * @return [type] [description] */ function duo_select($pdo,$table,$fields,$where='',$order=''){ $sql='SELECT '; if(is_array($fields)){ foreach($fields as $key){ $sql.=$key.','; } }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); //创建PDO预处理对象 $stmt = $pdo->prepare($sql); //die($yu->queryString);//查看 //执行查询操作 if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); //返回一个二维数组 return $stmt->fetchAll(); } } else { return false; } } }
点击 "运行实例" 按钮查看在线实例