批改状态:合格
老师批语:
4月26日作业:
func_pdo.php -- " SELECT " " WHERE " " FRPM " 等左右引号的空格容易忽视
die($sql) 很管用
代码:
test.php
<meta charset="utf-8"> <?php echo '<h2>作业:数据库操作函数库测试脚本</h2>'; //1.导入数据库操作函数库 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, 'salary'=>5900]; // $data = ['name'=> '刘老师']; // insert($pdo,$table,$data); //3.更新测试 // $table = 'staff'; // $where='id=24'; //查询条件使用字符串直接传入 // $data = ['name'=>'张良辉','sex'=>1, 'salary'=>5000]; // update($pdo,$table,$data,$where); //4.单条查询测试 // $table = 'staff'; // $fields = ['id','name','salary']; // $fields = '*'; // $fields = 'name,id'; // $where = 'salary > 4000'; // echo '<pre>'.print_r(find($pdo, $table, $fields, $where),true).'</pre>'; //5.多条查询测试 // $table = 'staff'; // $fields = ['id', 'name','salary']; // $fields = '*'; // $where = 'id > 12'; // $order = 'id asc'; // echo '<pre>'.print_r(select($pdo, $table, $fields, $where, $order),true).'</pre>'; //6.删除测试 $table = 'staff'; $where = 'id = 26'; //$where = 'id = 5'; delete($pdo, $table, $where);
点击 "运行实例" 按钮查看在线实例
func_pdo,php
<meta charset="utf-8">
<h2> PDO 增、删、改、查</h2>
<?php
echo '<h3>1、连接数据库</h3>';
//1.连接数据库
//判断数据库连接
if(!function_exists('connect')){
/**
* [connect description]
* @param [type] $dbname [description]
* @param string $type [description]
* @param string $host [description]
* @param string $charset [description]
* @param integer $post [description]
* @param string $user [description]
* @param string $pass [description]
* @return [type] [description]
*/
//连接数据库函数
function connect($dbname, $type='mysql', $host='127.0.0.1', $charset='utf8', $port=3306, $user='root', $pass='root'){
//(1).数据源
$dsn = "{$type}:host={$host}; dbname={$dbname}; charset={$charset}; port={$port}";
//(2).数据库用户名
$userName = 'root';
//(3).数据库用户密码
$password = 'root';
//(4).配置连接属性
$options = [
//1).设置错误模式
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
//2).数据表字段保持不变
PDO::ATTR_CASE => PDO::CASE_NATURAL,
//3).启用PDO模拟
PDO::ATTR_EMULATE_PREPARES => true,
//4).启用持久性连接
PDO::ATTR_PERSISTENT => true,
];
try{
//(5).实例化PDO类,创建PDO对象
$pdo = new PDO($dsn, $userName, $password, $options);
} catch (PDOException $e) {
die('Connect ERROR!:'.$e->getMessage());
}
return $pdo;
}
}
echo '<h3>2、新增数据</h3>';
// 2.新增数据
// 判断新增函数
if(!function_exists('insert')){
/**
* [insert description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $data [description]
* @return [type] [description]
*/
//新增数据函数
function insert($pdo, $table, $data=[]){
//(1).创建SQL语句
$sql = "INSERT IGNORE {$table} SET ";
//(2).循环键名(字段)
foreach (array_keys($data) as $field) {
$sql .= $field.'=:'.$field.',';
}
//(3).去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',').';';
//(4).创建PDO预处理对象
$stmt = $pdo->prepare($sql);
//(5).循环绑定参数到预处理对象
foreach ($data as $field => $value) {
$stmt->bindValue(":{$field}",$value);
}
//(6).执行新增操作
if($stmt->execute()){
if($stmt->rowCount()>0){
return true;
}
} else {
return false;
}
}
}
echo '<h3>3、修改数据</h3>';
//3.修改数据
//判断修改函数
if(!function_exists('update')){
/**
* [update description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $data [description]
* @param string $where [description]
* @return [type] [description]
*/
//判断修改函数
function update($pdo, $table, $data=[], $where=''){
//(1).创建SQL语句
$sql = "UPDATE {$table} SET ";
//循环键名(字段)
foreach (array_keys($data) as $field) {
$sql .= $field.'=:'.$field.',';
}
//(2).去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',');
//(3).添加更新条件
if(!empty($where)) {
$sql .= ' WHERE '. $where;
}else{
exit('条件不能为空');
}
//(4).创建PDO预处理对象
$stmt = $pdo->prepare($sql);
//(5).绑定参数到预处理对象
foreach ($data as $field => $value) {
$stmt->bindValue(":{$field}",$value);
}
//(6).执行更新操作
if($stmt->execute()){
if($stmt->rowCount()>0){
return true;
}
} else {
return false;
}
}
}
echo '<h3>4、查询单条数据</h3>';
//4.查询单条数据
//判断单条数据查询
if(!function_exists('find')){
/**
* [find description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $fields [description]
* @param string $where [description]
* @return [type] [description]
*/
//查询单条语句函数
function find($pdo, $table, $fields, $where=''){
//(1).创建SQL语句
$sql = ' SELECT ';
//循环判断单条数据
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field.', ';
}
} else {
$sql .= $fields.', ';
}
//(2).去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',');
//(3).添加查询条件
$sql .= ' FROM '.$table;
if(!empty($where)) {
$sql .= ' WHERE '.$where;
}
$sql .= ' LIMIT 1';
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',').';';
//die($sql);
//(4).创建PDO预处理对象
$stmt = $pdo->prepare($sql);
//(5).执行查询操作
if($stmt->execute()){
if($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetch();
}
} else {
return false;
}
}
}
echo '<h3>5、查询多条数据</h3>';
if(!function_exists('select')){
/**
* [select description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param [type] $filds [description]
* @param string $where [description]
* @param string $order [description]
* @return [type] [description]
*/
//判断多条语句查询
function select($pdo, $table, $fields, $where='', $order=''){
//(1).创建SQL语句
$sql = 'SELECT ';
//循环判断多条数据
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field.', ';
}
} else {
$sql .= $fields;
}
//(2).去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),', ');
$sql .= ' FROM '.$table;
//(2).添加查询条件
if(!empty($where)) {
$sql .= ' WHERE '. $where;
}
//添加排序条件
if(!empty($order)) {
$sql .= ' order by '.$order;
}
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),', ').';';
//(3).创建PDO预处理对象
$stmt = $pdo->prepare($sql);
//(4).执行查询操作
if($stmt->execute()){
if($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//返回一个二维数组
return $stmt->fetchAll();
}
} else {
return false;
}
}
}
echo '<h3>6、删除数据</h3>';
if(!function_exists('delete')){
/**
* [delete description]
* @param [type] $pdo [description]
* @param [type] $table [description]
* @param string $where [description]
* @return [type] [description]
*/
//删除数据函数
function delete($pdo, $table, $where=''){
//(1).创建SQL语句
$sql = "DELETE FROM {$table}";
//(2).添加删除条件
if(!empty($where)) {
$sql .= ' WHERE '. $where;
}else{
exit('条件不能为空');
}
//(3).去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),', ').';';
//(4).创建PDO预处理对象
$stmt = $pdo->prepare($sql);
//(5).执行删除操作
if($stmt->execute()){
if($stmt->rowCount()>0){
return true;
}
} else {
return false;
}
}
}
?>点击 "运行实例" 按钮查看在线实例
代码运行图片:




Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号