批改状态:未批改
老师批语:
本实例使用函数库的创建使用PDO对象的方式实现,该函数库功能包括数据库连接、新增记录、更新记录、单条查询、多条查询、记录删除功能。演示如下:
函数库文件:
<?php
/**
* PDO数据库操作函数库
*/
//连接数据库
if (!function_exists('connect'))
{
/**
* 数据库连接
* @param [type] $dbname [description]
* @param string $type [description]
* @param string $host [description]
* @param string $charset [description]
* @param string $port [description]
* @return [type] [description]
*/
function connect($dbname,$type='mysql',$host='127.0.0.1',$charset='utf8',$port='3306',$user='user',$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);
//简写
// $pdo = new PDO('mysql:dbname=php','root','root');
} catch(PDOException $e) {
print '连接错误'.$e->getMessage();
die();
}
return $pdo;
}
}
//新增数据
if (!function_exists('insert')) {
/**
* [insert description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param array $data [description]
* @return [type] [description]
*/
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 [type] $pdo [description]
* @param [type] $table [description]
* @param array $data [description]
* @return [type] [description]
*/
function update($pdo,$table,$data,$where)
{
//创建SQL语句
$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对象
$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')) {
/**
* [find description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $fields [description]
* @param [type] $where [description]
* @return [type] [description]
*/
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';
$sql = rtrim(trim($sql),',').';';
// die($sql);
// 创建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')) {
/**
* [select description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $fields [description]
* @param [type] $where [description]
* @param [type] $order [description]
* @return [type] [description]
*/
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;
}
$sql = rtrim(trim($sql),',').';';
// die($sql);
// 创建STMT对象
$stmt = $pdo->prepare($sql);
// die($stmt->queryString);
if ($stmt->execute()) {
if ($stmt->rowCount()>0) {
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetchALL();
}
} else {
return false;
}
}
}
//更新数据
if (!function_exists('delete')) {
/**
* [update description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $data [description]
* @param [type] $where [description]
* @return [type] [description]
*/
function delete($pdo,$table,$where)
{
//创建SQL语句
$sql = "DELETE FROM {$table} ";
//添加更新条件
if (!empty($where)) {
$sql .= ' WHERE '.$where;
} else {
exit('条件不能为空');
}
$sql = rtrim(trim($sql),',').';';
// die($sql);
//创建stmt对象
$stmt = $pdo->prepare($sql);
//执行删除操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
return true;
}
} else {
return false;
}
}
}测试文件脚本:
<?php /** * 数据库操作函数库测试脚本 */ require 'lib/func_pdo.php'; //1.连接测试 $type = 'mysql'; $host = '127.0.0.1'; $dbname = 'php'; $charset = 'utf8'; $port = 3306; $user = 'root'; $pass = 'root'; $pdo = connect($dbname,$type,$host,$charset,$port,$user,$pass); //2.新增测试 // $table = 'staff'; // $data = ['name'=>'赵本山','sex'=>0,'birthday'=>19590208,'salary'=>11000]; // insert($pdo,$table,$data); //3.更新测试 // $table = 'staff'; // $data = ['name'=>'赵子龙','sex'=>0,'birthday'=>19590208,'salary'=>11000]; // $where = 'staff_id=50'; // update($pdo,$table,$data,$where); //4.单条查询测试 // $table = 'staff'; // $fields = 'name,salary'; // $where = 'birthday < 19900101'; // echo "<pre>".print_r(find($pdo,$table,$fields,$where),true)."</pre>"; //5.多条查询测试 // $table = 'staff'; // $fields = 'name,salary'; // $fields = '*'; // $fields = ['name','birthday','salary']; // $where = 'birthday < 19900101'; // $order = 'birthday ASC'; // echo "<pre>".print_r(select($pdo,$table,$fields,$where,$order),true)."</pre>"; //6.删除测试 $table = 'staff'; $where = 'staff_id = 50'; delete($pdo,$table,$where);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号