Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。作业最好写点自己看法和总结。
class Db{
public $dns;
public $user;
public $password;
public $pdo;
function __construct($dns,$user,$password){
$this->dns = $dns;
$this->user = $user;
$this->password = $password;
//连接数据库
$this->content();
}
function content(){
$this->pdo = new PDO($this->dns,$this->user,$this->password);
}
function __destruct(){
$this->pdo = null;
echo '执行完毕,关闭连接';
}
}
$db = new Db('mysql:host=locaohost;dbname=film', 'root', 'root');
print_r($db->pdo); //结果为PDO Object ( ) ,连接数据库成功
$stmt = $db->pdo->prepare('SELECT * FROM `user` WHERE `id`>=:id');
$stmt -> execute(['id'=>1]);
$stmt -> setFetchMode(PDO::FETCH_ASSOC);
while( $user = $stmt->fetch() ){
print_r($user);
}
===================
print_r( $stmt->fetchAll() ); //或者全部调用
$stmt = $db->pdo->prepare('INSERT `user`(name,tel,password) VALUES(:name,:tel,:password)');
$stmt -> execute(['name'=>张明朗, 'tel'=>18080809999, 'password'=>sha1(123456)]);
$stmt = $db->pdo->prepare('UPDATE `user` SET `name`=:name, `tel`=:tel WHERE `id`=:id');
$stmt -> execute(['id'=>9,'name'=>'张明轩','tel'=>19980807777]);
$stmt = $db->pdo->prepare('DELECT FROM `user` WHERE `id`=:id ');
$stmt->execute(['id'=12]);