Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
namespace space1;
use \PDO;
use PDOException;
//对PDO二次封装
class MyPDO{
public $pdo;
public function __construct(array $info = array())
{
$type = $info['type'] ?? 'mysql';
$host =$info['host'] ?? '127.0.0.1';
$dbname = $info['dbname'] ?? 'blog';
$port = $info['port'] ?? 3308;
$user = $info['username'] ?? 'root';
$password = $info['password'] ?? 'root';
$dsn = "$type:host=$host;dbname=$dbname;port=$port";
try{
$this->pdo = new PDO($dsn,$user,$password);
}catch(PDOException $e){
echo '数据库连接错误:'.$e->getMessage();
}
}
}
//查询功能索引数组,绑定值
$obj = new MyPDO();
// $sql = 'SELECT * FROM `b_user` WHERE `id` =?';
// $sth = $obj->pdo->prepare($sql);
// $sth->bindValue(1,4,PDO::PARAM_INT);
// $sth->execute();
// $result = $sth->fetchAll();
// var_dump($result);
//查询功能关联数组,绑定变量
// $num = 4;
// $sql = 'SELECT * FROM `b_user` WHERE `id` = :id';
// $sth = $obj->pdo->prepare($sql);
// $sth->bindParam('id',$num,PDO::PARAM_INT);
// $sth->execute();
// $result = $sth->fetch();
// var_dump($result);
//查询功能 索引数组
// $sql = 'SELECT * FROM `b_user` WHERE `id` =?';
// $sth = $obj->pdo->prepare($sql);
// $data = [4];
// $sth->execute($data);
// $result = $sth->fetchAll();
// var_dump($result);
//查询功能 关联数组
// $sql = 'SELECT * FROM `b_user` WHERE `id` =:id';
// $sth = $obj->pdo->prepare($sql);
// $data = ['id'=>4];
// $sth->execute($data);
// $result = $sth->fetchAll();
// var_dump($result);
//更新功能
// $sql = 'UPDATE `b_user` SET u_username=:name WHERE `id`=:id';
// $sth = $obj->pdo->prepare($sql);
// $data = ['name' => 'Tom','id'=>4];
// if($sth->execute($data)){
// echo "更新成功";
// }else{
// echo "更新失败";
// }
//插入功能
// $sql = <<< SQL
// INSERT INTO `b_user`
// (`u_username`,`u_password`,`u_reg_time`,`u_is_admin`,`key`)
// VALUES(:name,:pass,:time,:is,:key);
// SQL;
// $sth = $obj->pdo->prepare($sql);
// $sth->bindValue('name','OOO',PDO::PARAM_STR);
// $sth->bindValue('pass','1234',PDO::PARAM_STR);
// $sth->bindValue('time','5540349',PDO::PARAM_STR);
// $sth->bindValue('is',0,PDO::PARAM_INT);
// $sth->bindValue('key','5540349',PDO::PARAM_STR);
// $sth->execute();
// echo $obj->pdo->lastInsertId();
//删除功能
$sql = 'DELETE FROM `b_user` WHERE `id` = :id';
$sth = $obj->pdo->prepare($sql);
$sth->bindValue('id',14,PDO::PARAM_INT);
$sth->execute();
if($sth->columnCount()){
echo "删除成功";
}else{
echo "删除失败";
}