Correction status:qualified
Teacher's comments:
<?php /** * */ if (!function_exists('connect')) { /** * @param $dbname * @param $type * @param $host * @param $charset * @param $port */ function connect($dbname,$type='mysql',$host='localhost',$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::ATTR_PERSISTENT => true, ]; try { $pdo = new PDO($dsn, $userName, $password, $options); // echo 'ok'; }catch (PDOException $e){ print '连接失败'.$e->getMessage(); die(); } return $pdo; } } if (!function_exists('insert')) { function insert($pdo, $table, $data=[]) { // insert ignore staff set name=:name, salary=:salary $sql = "INSERT IGNORE {$table} SET "; foreach (array_keys($data) as $field){ $sql .= $field.' =:'.$field.','; } $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('update')) { function update($pdo, $table, $data=[],$where='') { // update staff set name=:name, salary=:salary where staff_id=: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')) { function find($pdo, $table, $fields,$where='') { // select name from staff where staff_id=:where; $sql = "SELECT "; if(is_array($fields)){ foreach($fields as $field){ $sql .= $fields. ', '; } }else{ $sql .= $fields. ','; } $sql = rtrim(trim($sql), ','); $sql .= ' FROM '.$table; if(!empty($where)){ $sql .= ' WHERE '.$where.';'; } // $sql .= ' LIMIT 4;'; // die($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='') { // select name from staff where staff_id=:where; $sql = "SELECT "; if(is_array($fields)){ foreach($fields as $field){ $sql .= $fields. ', '; } }else{ $sql .= $fields. ','; } $sql = rtrim(trim($sql), ','); $sql .= ' FROM '.$table; if(!empty($where)){ $sql .= ' WHERE '.$where; } if(!empty($order)){ $sql .= ' ORDER BY '.$order.';'; }else{ $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='') { // update staff set name=:name, salary=:salary where staff_id=:where; $sql = "DELETE FROM {$table} "; 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; } } }
点击 "运行实例" 按钮查看在线实例