批改状态:未批改
老师批语:
MySQL面向对象操作
1./*查找数据*/
<?php
//设置输出字符集为utf-8
header("content-type:text/html;charset=utf-8");
//引入数据库配置文件,并打开数据库
include_once 'confing.php';
/*查找数据*/
//sql语句
$sql = " SELECT * FROM userinfo WHERE `id`=?; ";
//创建预处理对象
$stmt = $conn->prepare($sql);
//参数绑定
$id = 12;
$stmt->bind_param('i',$id);
//执行sql语句
if($stmt->execute()){
//获取结果集并放入缓存区
$stmt->store_result();
//将结果集绑定到变量上
$stmt->bind_result($id,$account,$password,$head);
if($stmt->num_rows > 0){
while($stmt->fetch()){
echo 'id'.'=>'.$id.'<br>','account'.'=>'.$account.'<br>','password'.'=>'.$password.'<br>','head'.'=>'.$head.'<br>';
}
}
}else{
echo $stmt->errno.':'.$stmt->error;
}
$stmt->close();
$conn->close();
?>点击 "运行实例" 按钮查看在线实例
2./*插入数据*/
<?php
//设置输出字符集为utf-8
header("content-type:text/html;charset=utf-8");
//引入数据库配置文件,并打开数据库
include_once 'confing.php';
/*插入一条数据*/
//准备sql语句:带有占位符
$sql = " INSERT INTO `userinfo` SET `account` = ?,`password` = ?; ";
//创建一个sql语句的预处理对象
$stmt = $conn->prepare($sql);
//参数绑定
$acconut = '小张';
$password = 'xz55621';
$stmt->bind_param('ss',$acconut,$password);
echo $sql;
//执行sql语句
$result = $stmt->execute();//不要给定参数,默认执行准备sql语句
if($result){
if($stmt->affected_rows>0){//执行结果影响几行
echo '成功插入'.$stmt->affected_rows.'条数据'.'插入id为:'.$stmt->insert_id;
}else{
echo '没有新增记录';
}
}else{
//执行错误,查看错误原因
exit($stmt->errno.':'.$stmt->error);
}
//注销准备语句
$stmt->close();
//注销数据库连接对象
$conn->close();
?>点击 "运行实例" 按钮查看在线实例
/*3.插入多条数据*/
<?php
//设置输出字符集为utf-8
header("content-type:text/html;charset=utf-8");
//引入数据库配置文件,并打开数据库
include_once 'confing.php';
/*插入多条数据*/
//准备sql语句:带有占位符
$sql = " INSERT INTO `userinfo` SET `account` = ?,`password` = ?; ";
//创建一个sql语句的预处理对象
$stmt = $conn->prepare($sql);
//参数绑定
$data = array(
array('acconut' => '张飞', 'password' => '2565156'),
array('acconut' => '赵云', 'password' => '55225565'),
array('acconut' => '李典', 'password' => '789845622'),
);
$stmt->bind_param('ss',$acconut,$password);
foreach($data as $userinfo){
$acconut = $userinfo['acconut'];
$password = $userinfo['password'];
$result = $stmt->execute();
if($result){
if($stmt->affected_rows>0){//执行结果影响几行
echo '成功插入'.$stmt->affected_rows.'条数据'.'插入id为:'.$stmt->insert_id;
}else{
echo '没有新增记录';
}
}else{
//执行错误,查看错误原因
exit($stmt->errno.':'.$stmt->error);
}
}
//注销准备语句
$stmt->close();
//注销数据库连接对象
$conn->close();
?>点击 "运行实例" 按钮查看在线实例
/*4.更新数据*/
<?php
//设置输出字符集为utf-8
header("content-type:text/html;charset=utf-8");
//引入数据库配置文件,并打开数据库
include_once 'confing.php';
/*更新数据*/
//sql语句
$sql = " UPDATE userinfo SET `account`=? where `id`=?; ";
//创建预处理对象
$stmt = $conn->prepare($sql);
//参数绑定
$account = '李逵';
$id = 8;
$stmt->bind_param('si',$account,$id);
//执行sql语句
$result = $stmt->execute();
if($result){
if($stmt->affected_rows>0){
echo '成功更新'.$stmt->affected_rows.'条数据';
}else{
echo '没有更新数据';
}
}else{
echo $stmt->errno.':'.$stmt->error;
}
?>点击 "运行实例" 按钮查看在线实例
/*5.删除数据*/
<?php
//设置输出字符集为utf-8
header("content-type:text/html;charset=utf-8");
//引入数据库配置文件,并打开数据库
include_once 'confing.php';
/*删除数据*/
//sql语句
$sql = " DELETE FROM userinfo WHERE `id`=?; ";
//创建预处理对象
$stmt = $conn->prepare($sql);
//参数绑定
$id = 4;
$stmt->bind_param('i',$id);
//执行sql语句
$result = $stmt->execute();
if($result){
if($stmt->affected_rows>0){
echo '删除'.$stmt->affected_rows.'条数据';
}else{
echo '没有删除数据';
}
}else{
echo $stmt->errno.':'.$stmt->error;
}
?>点击 "运行实例" 按钮查看在线实例
/*6.PDO的优势是什么?
1.提供统一接口,更换数据库不用麻烦的配置,更换数据库,内部代码改变量极少
2.绑定参数,防止sql注入,只编译一次,可以使用不同的参数,重复操作(查询语句)
缩短编译周期,使用更少的资源,而且运行更快
3.PDO支持存储过程中的调度等高性能db操作
4.支持多数库,兼容性好,可扩展性强
*/
/*7.PDO数据库连接*/
<?php
/*PDO连接数据库*/
$dsn = 'mysql:host=localhost;dbname=test';
$user = 'root';
$pass = 'root';
try{
//设置数据库字符集
$set_char = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
//连接数据库
$pdo = new PDO($dsn,$user,$user,$set_char);
}catch(PDOException $e){
//捕捉错误信息
die('连接失败:'.$e->getMessage());
}
?>点击 "运行实例" 按钮查看在线实例
/*8.PDO数据库数据操作*/
/*PDO连接数据库,进行插入数据*/
require 'configPDO.php';
//准备sql语句
$sql = "INSERT INTO `userinfo` SET `account`=:account,`password`=:password";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定参数
$data = array(':account'=>'小黑',':password'=>'25551525hg');
$stmt->bindParam(':account',$data[':account'],PDO::PARAM_STR);
$stmt->bindParam(':password',$data[':password'],PDO::PARAM_STR);
//执行sql
if($stmt->execute()){//返回一个数组
if($stmt->rowCount()>0){
echo '成功插入'.$stmt->rowCount().'条数据';
}else{
echo '没有插入数据';
}
}else{
echo "插入数据失败";
print_r($stmt->errorInfo());
}
/*插入多条数据*/
//数据源
$dataInsertMore = array(
array(':account'=>'刘备',':password'=>'liubei',':head'=>'26321.jpg'),
array(':account'=>'黄忠',':password'=>'huangzhong',':head'=>'62581.jpg'),
array(':account'=>'夏侯惇',':password'=>'xiahoudui',':head'=>'62142.jpg'),
);
//准备sql语句
$sqlInsertMore = "INSERT INTO `userinfo` (`account`,`password`,`head`) VALUES";
foreach($dataInsertMore as $arr){
$sqlInsertMore.="("." '{$arr[':account']}','{$arr[':password']}','{$arr[':head']}' ),";
}
$sqlInsertMore = substr($sqlInsertMore,0,strlen($sqlInsertMore)-1);
//echo $sqlInsertMore;
//创建预处理对象
$stmtInsertMore = $pdo->prepare($sqlInsertMore);
//执行
if($stmtInsertMore->execute()){
if($stmtInsertMore->rowCount()>0){
echo '插入'.$stmtInsertMore->rowCount().'条数据';
}else{
echo '没有插入数据!';
}
}else{
echo "插入数据失败".$stmtInsertMore->errorInfo();
}
/*删除数据*/
//准备sql语句
$sql = " DELETE FROM userinfo WHERE id>:idOne AND id<:idTwo";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//数据绑定
$idOne = 15;
$idTwo = 21;
$stmt->bindParam(':idOne',$idOne);
$stmt->bindParam(':idTwo',$idTwo);
//执行
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '成功删除'.$stmt->rowCount().'条数据';
}else{
echo '删除未成功';
}
}else{
echo '删除数据出错:'.print_r($stmt->errorInfo());
}
/*更新数据*/
//准备sql语句
$sql = " UPDATE `userinfo` SET account=:account WHERE id=:id";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定数据
$id = 2;
$account = '烟花';
$stmt->bindParam(':account',$account,PDO::PARAM_STR);
$stmt->bindParam(':id',$id);
//执行
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '成功更新'.$stmt->rowCount().'条数据';
}else{
echo '没有更新数据';
}
}else{
echo '更新数据出错:'.$stmt->errorInfo();
}
/*查找数据*/
$id = 'id';
$account = 'account';
//准备sql 语句
$sql = " SELECT `{$id}`,`{$account}` FROM `userinfo` WHERE `id`>:idOne ";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定参数
$idOne = 9;
$stmt->bindParam(':idOne',$idOne);
//执行预处理
if($stmt->execute()){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
print_r($row);
}
}else{
echo '查找数据出错'.print_r($tmt->errorInfo());
}点击 "运行实例" 按钮查看在线实例
/*9.计算查询结果数量*/
<?php
/*
PDO 获取查询结果记录数量的正确方式?
rowcount()函数返回的是执行数据库操作之后,影响的行数,delete,update,insert
等操作会实质性的影响数据表的改变。但是select查询操作,不影响表的改变,
所以rowcount()函数此时会失效,但是有时会返回正确的查询函数,因为这个操作会把
结果集存入内存中,然后计算查询到的行数,但是大部分数据库只会把部分数据返还,
然后再返回剩余数据,此时rowcount()就算行数不对,此时要用select count(*) from table
*/
$id = 'id';
$account = 'account';
//准备sql 语句
$sql = " SELECT * FROM `userinfo` WHERE `id`>:idOne ";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定参数
$idOne = 6;
$stmt->bindParam(':idOne',$idOne);
//执行预处理
if($stmt->execute()){
echo '查询到'.$stmt->rowCount().'条数据';
}else{
echo '查询出错:'.print_r($stmt->errorInfo());
}
?>点击 "运行实例" 按钮查看在线实例
/*10.将查询结果绑定到变量上*/
<?php
/*将查询结果绑定到变量上*/
//准备sql 语句
$sql = " SELECT `account`,`password` FROM `userinfo` WHERE `id`>:idOne ";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定参数
$idOne = 9;
$stmt->bindParam(':idOne',$idOne);
$stmt->bindColumn('account',$account);
$stmt->bindColumn('password',$password);
//执行预处理
if($stmt->execute()){
while($row = $stmt->fetch()){
echo $account.'<br>'.$password.'<hr>';
}
}else{
echo '查找数据出错'.print_r($tmt->errorInfo());
}
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号