Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续努力。
<?php
$db = [
'type' => 'mysql',
'host' => '127.0.0.1',
'dbname' => 'mt',
'username' => 'root',
'password' => '111',
];
$dsn = "{$db['type']}:host = {$db['host']};dbname = {$db['dbname']}";
try{
$pdo = new PDO($dsn,$db['username'],$db['password']);
echo "Connection Success!";
}catch (PDOException $e){
die('Connection Failed!' . $e -> getMessage());
}
<?php
//1.连接
require __DIR__.'/connect.php';
//2.创建sql语句模板
$sql = 'INSERT INTO `category` SET `name` = :name , `alias` = :alias';
//3.创建sql语句对象
$stmt = $pdo ->prepare($sql);
//4.绑定参数
$name = 'bjst';
$alias = '巴勒斯坦';
$stmt ->bindParam('name',$name,PDO::PARAM_STR);
$stmt ->bindParam('alias',$alias,PDO::PARAM_STR);
//5.执行查询
if($stmt -> execute()){
if($stmt -> rowCount() > 0){
echo '新增记录'.$stmt -> rowCount().'条';
}
}else{
die($stmt -> errorInfo());
}
//6.关闭连接
$pdo = null;
<?php
//1.连接
require __DIR__.'/connect.php';
//2.创建sql语句模板
$sql = 'UPDATE `category` SET `alias` = :alias WHERE `cate_id` = 4';
//3.创建sql语句对象
$stmt = $pdo -> prepare($sql);
//4.绑定参数
$alias = '巴基斯坦';
$stmt ->bindParam('alias',$alias,PDO::PARAM_STR);
//5.执行查询
if($stmt -> execute()){
if($stmt -> rowCount() > 0){
echo '更新记录'.$stmt -> rowCount().'条';
}
}else{
die($stmt -> errorInfo());
}
//6.关闭连接
$pdo = null;
<?php
//1.连接
require __DIR__ . '/connect.php';
//2.创建sql语句模板
$sql = 'SELECT * FROM `category` WHERE `cate_id` > :cate_id';
//3.创建sql语句对象
$stmt = $pdo -> prepare($sql);
//4.绑定参数
$cate_id = 2;
$stmt -> bindParam('cate_id',$cate_id,PDO::PARAM_INT);
//5.执行查询
if ($stmt -> execute()){
if ($stmt -> rowCount() > 0){
$cates = $stmt -> fetchAll(PDO::FETCH_BOTH);
foreach ($cates as $cate){
echo '<pre>';
print_r($cate);
}
}
}else{
die($stmt -> errorInfo());
}
//6.关闭连接
$pdo = null;
<?php
//1.连接
require __DIR__ . '/connect.php';
//2.创建sql语句模板
$sql = 'DELETE FROM `category` WHERE `cate_id` = :cate_id';
//3.创建sql语句对象
$stmt = $pdo->prepare($sql);
//4.绑定参数
$cate_id = 4;
$stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
//5.执行查询
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo '删除记录' . $stmt->rowCount() . '条';
}
} else {
die('$stmt -> errorInfo()');
}
//6.关闭连接
$pdo = null;
绑定参数也可以这样:
$stmt -> execute([‘name’ => $name,’alias’ => $alias]);