Correction status:qualified
Teacher's comments:
<?php // 连接数据库 if (!function_exists('connect')) { 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 = 'gakkispy'; $password = 'password'; $options = [ PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_CASE=>PDO::CASE_NATURAL, PDO::ATTR_EMULATE_PREPARES=>true, PDO::ATTR_PERSISITENT=>true, ]; try{ $pdo = new PDO($dsn, $userName, $password, $options); } catch (PDOException $err) { die('连接错误:' . $err->getMessage()); } return $pdo; } } // 插入 if (!function_exists('insert')) { function insert( $pdo, $table, $data = []) { // 创建 sql 语句 $sql = "INSERT IGNORE {$table} SET"; foreacg (array_keys($data) as $field) { $sql .= $field . '=:' . $field . ', '; } $sql = rtrim(trim($sql), ','). ';'; $stmt = $pdo->prepare($sql); foreach ($data as $keys => $value) { $stmt->bindValue(":{$keys", $value); }; // 执行 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } }else { return false; } } } // 删除 if (!function_exsits('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($smtm->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 $key) { $sql .= $key .'=:' . $key . ', '; } $sql = rtrim(trim($sql), ','); if (!empty($where)) { $sql .= ' WHERE ' . $where; } else { exit('条件不能为空'); } $stmt = $pdo->prepare($sql); foreach($data as $key=>$value) { $stmt->bindValue(":{$key}", $value); } if ($stmt->execute()) { if ($stmt->rowCount()>0) { return true; } } else { return false; } } } // 查询 单条 if (!function_exits('find')) { function find($pdo, $table, $fields,$where='') { $sql = 'SELELCT'; 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), ',') . ';'s; $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; } } }
点击 "运行实例" 按钮查看在线实例