实例
<?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 = 'root'; //数据库用户名 $password = 'root'; //数据库用户密码 //配置连接属性 $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); } catch (PDOException $e) { die('Connect ERROR!:'.$e->getMessage()); } return $pdo; } } //插入函数 if(!function_exists('insert')) { 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 = $pdo->prepare($sql); foreach ($data as $keys => $value) { $stmt->bindValue(":{$keys}", $value); } ; //执行新增操作 // $stmt->execute(); if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } } else { return false; } } } //删除 if(!function_exists('delete')){ function delete($pdo,$table,$where='') { $sql = "DELETE FROM {$table} "; if(!empty($where)){ $sql .= 'WHERE '. $where; print_r($sql); }else{ exit('条件不能为空'); } $sql = rtrim(trim($sql),',').';'; //创建预处理对象 $stmt=$pdo->prepare($sql); if($stmt->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_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; } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例