Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
<?php
//pdo连接
//第一个参数是一个组合
//mysql:标识
//host=地址
//dbname=库名
//$pdo=new PDO('mysql:host:127.0.0.1;dbname=demo','root','root');
//print_r($pdo);
//数据库连接参数
$db =[
'type'=>'mysql',
'host'=>'localhost',
'dbname'=>'demo',
'username'=>'root',
'password'=>'root',
'part'=> 3306
];
$dsn ="{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
//$pdo =new PDO($dsn,$db['username'],$db['password']);
//print_r($pdo);
//报错时常用
try{
$pdo = new PDO($dsn,$db['username'],$db['password']);
}catch(PDOException $e){
die($e->getMessage());
}
<?php
require __DIR__.'/demo1.php';
//尽可能把参数变成可变的值,所以可以让自己更变名称
//创建sql语句模板
$sql ='INSERT INTO `demo1`SET `id`=:i';
//预处理
$stmt = $pdo ->prepare($sql);
//stmt是什么?是预处理语句
//要绑定语句,需要对预处理语句的参数进行设定
$stmt ->bindParam('i',$id,PDO::PARAM_INT);
$add =$stmt ->execute();
if($add){
$count =$stmt -> rowCount();
if($count>0){
echo'数据插入成功';
}else{
echo '数据插入失败';
}
}else{
die(print_r($stmt-> errorInfo(),ture));
}
$pdo = null;
print_r($pdo);
<?php
require __DIR__.'/demo1.php';
$sql = 'UPDATE `demo1` SET `name`=:n,`id`=:i
WHERE `state`=:s';
$stmt = $pdo ->prepare($sql);
$name='外公司';
$id='3';
$state='0';
$stmt->bindParam('n',$name,PDO::PARAM_STR);
$stmt->bindParam('i',$id,PDO::PARAM_STR);
$stmt->bindParam('s',$state,PDO::PARAM_STR);
$add = $stmt->execute();
if($add){
$count = $stmt-> rowCount();
if($count>0){
echo '数据插入成功';
}else{
echo'插入失败';
}
}else{
die(print_r($stmt-> errorinfo(),true));
}
<?php
require __DIR__.'/demo1.php';
$sql ='SELECT * FROM `demo1`';
$stmt =$pdo -> prepare($sql);
$add= $stmt ->execute();
if ($add){
print_r($stmt->fetchAll());
}else{
die(print_r($stmt-> errorInfo(),true));
}