Correction status:Uncorrected
Teacher's comments:
数据库常用操作
连接,新增,更新,单条查询,多条查询,删除
<?php /** * PDO数据库操作函数库 */ //连接数据库 if (!function_exists('connect')) { /** * 数据库连接 * @param $dbname * @param string $type * @param string $host * @param string $charset * @param int $port */ 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 = new PDO($dsn, $userName, $password, $options); } catch (PDOException $e) { print '连接错误'.$e->getMessage(); die(); } return $pdo; } } //新增数据 if (!function_exists('insert')) { /** * 新增数据 * @param $pdo * @param $table * @param array $data */ function insert($pdo, $table, $data=[]) { //创建SQL语句 $sql = "INSERT IGNORE {$table} SET "; foreach (array_keys($data) as $field) { $sql .= $field.' =:'.$field.','; } $sql = rtrim(trim($sql),',').';'; //创建stmt对象 $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')) { /** * 更新数据 * @param $pdo * @param $table * @param array $data */ function update($pdo, $table, $data=[], $where='') { //创建SQL语句 //insert ignore staff set name=:name,salary=:salary; $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对象 $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;'; //创建STMT对象 $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 * @param string $order * @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.';'; } //创建STMT对象 $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 array $data */ function delete($pdo, $table, $where='') { //创建SQL语句 $sql = "DELETE FROM {$table}"; //添加删除条件 if (!empty($where)) { $sql .= ' WHERE '. $where .';'; } else { exit('条件不能为空'); } //创建stmt对象 $stmt = $pdo->prepare($sql); //执行删除操作 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } } else { return false; } } }
点击 "运行实例" 按钮查看在线实例
总结:
注意SQL语句的结尾是分号
可用语句$sql = rtrim(trim($sql),',').';';来实现