Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:看十遍, 不如动手写一遍
namespace chapter2;
//1.连接数据库
$mysqli=new \mysqli('localhost','root','root','phpstudy');
//2.sql查询操作
//准备一条sql语句
$sql='insert `users` set `name`=?,`email`=?,`password`=?,`register_time`=?';
//创建预处理对象,通过stmt->init方法
$stmt=$mysqli->stmt_init();
//创建sql语句对象
$stmt->prepare($sql);
//变量与sql语句中的占位符绑定 bind_param()
$user=['qiqi','qiqi@qq.com',sha1(123456),time()];
list($name,$email,$password,$register_time)=$user;
$stmt->bind_param('sssi',$name,$email,$password,$register_time);
//执行
$stmt->execute();
//3.结果处理
if($stmt->affected_rows===1):
echo '增加成功,增加的主键id为'.$stmt->insert_id;
else:
echo '增加失败'.$stmt->error;
endif;
//4.结束操作 关闭数据库连接
$stmt->close();
//更新操作
namespace chapter2;
//1.连接数据库
$mysqli=new \mysqli('localhost','root','root','phpstudy');
//2.sql查询操作
//准备一条sql语句
$sql='update `users` set `name`=?,`email`=? where `id`=?';
//创建预处理对象,通过stmt->init方法
$stmt=$mysqli->stmt_init();
//创建sql语句对象
$stmt->prepare($sql);
//变量与sql语句中的占位符绑定 bind_param()
$user=['qiqi','qiqi@qq.com',9];
list($name,$email,$id)=$user;
$stmt->bind_param('ssi',$name,$email,$id);
//执行
$stmt->execute();
//3.结果处理
if($stmt->affected_rows===1):
echo '更新成功';
else:
echo '更新失败'.$stmt->error;
endif;
//4.结束操作 关闭数据库连接
$stmt->close();
//删除操作
namespace chapter2;
//1.连接数据库
$mysqli=new \mysqli('localhost','root','root','phpstudy');
//2.sql查询操作
//准备一条sql语句
$sql='delete from `users`where `id`=?';
//创建预处理对象,通过stmt->init方法
$stmt=$mysqli->stmt_init();
//创建sql语句对象
$stmt->prepare($sql);
//变量与sql语句中的占位符绑定 bind_param()
//$user=[9];
//list($id)=$user;
$id=9;
$stmt->bind_param('i',$id);
//执行
$stmt->execute();
//3.结果处理
if($stmt->affected_rows===1):
echo '删除成功';
else:
echo '删除失败'.$stmt->error;
endif;
//4.结束操作 关闭数据库连接
$stmt->close();
//查询操作
namespace chapter2;
//查询1:用STMT对象查询
//1.连接数据库
$mysqli=new \mysqli('localhost','root','root','phpstudy');
//2.sql查询操作
//准备一条sql语句
$sql='select `id`,`name`,`email` from `users`where `id`>?';
//创建预处理对象,通过stmt->init方法
$stmt=$mysqli->stmt_init();
//创建sql语句对象
$stmt->prepare($sql);
//变量与sql语句中的占位符绑定 bind_param()
//$user=[9];
//list($id)=$user;
$id=2;
$stmt->bind_param('i',$id);
//执行
$stmt->execute();
//3.结果处理
//bind_result():将结果集中的字段(列),绑定到指定的变量上
$stmt->bind_result($id,$name,$email);
while ($stmt->fetch())
{
echo "$id===>$name===>$email<br>";
}
//4.结束操作 关闭数据库连接
//释放结果集
$stmt->free_result();
$stmt->close();
//查询操作
namespace chapter2;
//查询2:用结果集对象查询
//1.连接数据库
$mysqli=new \mysqli('localhost','root','root','phpstudy');
//2.sql查询操作
//准备一条sql语句
$sql='select `id`,`name`,`email` from `users`where `id`>?';
//创建预处理对象,通过stmt->init方法
$stmt=$mysqli->stmt_init();
//创建sql语句对象
$stmt->prepare($sql);
//变量与sql语句中的占位符绑定 bind_param()
//$user=[9];
//list($id)=$user;
$id=3;
$stmt->bind_param('i',$id);
//执行
$stmt->execute();
//3.结果处理
$result=$stmt->get_result();
//$user=$result->fetch_array();
//$user=$result->fetch_row();
//$user=$result->fetch_assoc();
while ($user=$result->fetch_assoc())
{
echo "{$user['id']}==>{$user['name']}==>{$user['email']} <br>";
}
echo "<br>";
$result->data_seek(0);
$users=$result->fetch_all(MYSQLI_ASSOC);
foreach ($users as $user)
{
echo "{$user['id']}==>{$user['name']}==>{$user['email']} <br>";
}
print_r($users);
//4.结束操作 关闭数据库连接
//释放结果集
$result->free_result();
$stmt->close();
namespace chapter2;
//配置接口常量
interface iDbParam
{
//主机名
const HOST="localhost";
//数据库类型
const TYPE="mysql";
//默认数据库名称
const DBNAME="phpstudy";
//数据库数据库账号
const DB_NAME="root";
//数据库数据库密码
const DB_PASSWORD="root";
//默认编码字符集
const CHARSET="utf8";
}
//接口构造方式:实例化数据库连接对象
interface iDBLink
{
public function __construct(...$linkParams);
}
//接口方法,允许多重继承
interface iCURD extends iDbParam,iDBLink
{
//新增操作
public function insert(array $data);
//更新操作
public function update(array $date,string $where='');
//查询操作
public function select(string $where);
//删除操作
public function delete(string $where);
}
namespace chapter2;
//用PDO来实现数据库操作
//加载iCUDR接口文件
use PDO;
require "demo7.php";
class Db_PDO implements iCURD
{
// 新增pdo操作对象属性
private $pdo=null;
public function __construct(...$linkParams)
{
list($dsn,$username,$password)=$linkParams;
$this->pdo=new PDO($dsn,$username,$password);
}
//新增操作
public function insert(array $data)
{
$sql="insert `users` set `name`=?,`email`=?,`password`=?,`register_time`=?;";
$stmt=$this->pdo->prepare($sql);
$stmt->execute($data);
return $stmt->rowCount()===1?'新增成功':'新增失败';
}
//更新操作
public function update(array $date,string $where='')
{
//生成更新字符串
$param='';
foreach($date as $key =>$value)
{
$param.="`{$key}`='{$value}',";
}
$param=rtrim($param,',');
$sql="update `users` set {$param} where {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount()===1?'更新成功':'更新失败';
}
//查询操作
public function select(string $where)
{
$where=empty($where)?$where:'where '.$where;
$sql="select * from `users` {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//删除操作
public function delete(string $where)
{
$sql="delete from `users` where {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount()===1?'删除成功':'删除失败';
}
}
namespace chapter2;
//用MYSQLI来实现数据库操作
use mysqli;
//加载iCUDR接口文件
require "demo7.php";
class Db_MySQLI implements iCURD
{
// 新增pdo操作对象属性
private $mysqli=null;
public function __construct(...$linkParams)
{
list($host,$username,$password,$dbname)=$linkParams;
$this->mysqli=new mysqli($host,$username,$password,$dbname);
}
//新增操作
public function insert(array $data)
{
$sql="insert `users` set `name`=?,`email`=?,`password`=?,`register_time`=?;";
$stmt=$this->mysqli->prepare($sql);
list($name,$email,$password,$register_time)=$data;
$stmt->bind_param('sssi',$name,$email,$password,$register_time);
$stmt->execute();
return $stmt->affected_rows===1?'新增成功':'新增失败';
}
//更新操作
public function update(array $date,string $where='')
{
//生成更新字符串
$param='';
foreach($date as $key =>$value)
{
$param.="`{$key}`='{$value}',";
}
$param=rtrim($param,',');
$sql="update `users` set {$param} where {$where}";
$stmt=$this->mysqli->prepare($sql);
$stmt->execute();
return $stmt->affected_rows===1?'更新成功':'更新失败';
}
//查询操作
public function select(string $where)
{
$where=empty($where)?$where:'where '.$where;
$sql="select * from `users` {$where}";
$stmt=$this->mysqli->prepare($sql);
$stmt->execute();
return $stmt->get_result();
}
//删除操作
public function delete(string $where)
{
$sql="delete from `users` where {$where}";
$stmt=$this->mysqli->prepare($sql);
$stmt->execute();
return $stmt->affected_rows===1?'删除成功':'删除失败';
}
}
namespace chapter2;
//通用类
class DB
{
public static function insert(iCURD $db,array $data)
{
return $db->insert($data);
}
//更新操作
public static function update(iCURD $db,array $date,string $where='')
{
return $db->update($date,$where);
}
//查询操作
public static function select(iCURD $db,string $where)
{
return $db->select($where);
}
//删除操作
public static function delete(iCURD $db,string $where)
{
return $db->delete($where);
}
}
namespace chapter2;
//用pdo方式来操作数据库
//载入Db_PDO类
require 'demo8.php';
//载入通用类
require 'demo10.php';
$dsn=iDbParam::TYPE.':host='.iDbParam::HOST.';dbname='.iDbParam::DBNAME.';charset='.iDbParam::CHARSET ;
$link=new Db_PDO($dsn,iDbParam::DB_NAME,iDbParam::DB_PASSWORD);
//增加操作
//echo DB::insert($link,['qi','qi@qq.com',sha1('123456'),time()]);
//更新操作
//echo DB::update($link,['name'=>'芙蓉','email'=>'furong@qq.com'],'id=10');
//查询操作
//foreach (DB::select($link,'id>5') as $value)
//{
// echo "{$value['name']}===={$value['email']}<br>";
//}
//删除操作
echo DB::delete($link,'id=12');
namespace chapter2;
//用mysqli方式来操作数据库
//载入Db_MYSQLI类
require 'demo9.php';
//载入通用类
require 'demo10.php';
$link=new Db_MySQLI(iDbParam::HOST,iDbParam::DB_NAME,iDbParam::DB_PASSWORD,iDbParam::DBNAME);
//增加操作
//echo DB::insert($link,['张小石','xiaoshi@qq.com',sha1('123456'),time()]);
//更新操作
//echo DB::update($link,['name'=>'木子','email'=>'muzi@qq.com'],'id=14');
//查询操作
//foreach (DB::select($link,'id>5') as $value)
//{
// echo "{$value['name']}===={$value['email']}<br>";
//}
//删除操作
echo DB::delete($link,'id=11');
where
关键字与查询条件之间,没有空格导致查询失败。新增查询的占位符,如果和传入的个数不同,会导致新增失败。