Correction status:qualified
Teacher's comments:
<?php if (!function_exists('connect1')){ function connect1( $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 'OK'; } catch (PDOException $e) { die('Connect ERROR!:'.$e->getMessage()); } return $pdo; } } if(!function_exists('inster1')){ function inster1($pdo,$table,$data=[]){ $sql = "INSERT IGNORE {$table} SET "; foreach (array_keys($data) as $field) { $sql .= $field.'=:'.$field.', '; } $sql = rtrim(trim($sql),',').';'; // echo $sql;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('update1')) { function update1($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 = 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),',').';'; $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; } } } /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/1 0001 * Time: 21:12 */
点击 "运行实例" 按钮查看在线实例