Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 匿名参数+索引数组
$sql = 'INSERT `staff` SET `name`= ?,`sex`= ?,`email`= ?;';
$stmt = $db->prepare($sql);
$data = ['小明', 1, 'abc@qq.com'];
// 执行
$stmt->execute($data);
echo "匿名参数+索引数组:新增了" . $stmt->rowCount() . "条记录";
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 命名参数+关联数组
$sql = 'INSERT `staff` SET `name`= :name,`sex`= :sex,`email`= :email;';
$stmt = $db->prepare($sql);
$data = ['name' => '小红', 'sex' => 0, 'email' => 'bcd@qq.com'];
// 执行
$stmt->execute($data);
echo "命名参数+关联数组:新增了" . $stmt->rowCount() . "条记录";
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 参数绑定--值绑定
$sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
$stmt = $db->prepare($sql);
list($name, $sex, $email) = ['小丽', 1, 'cba@qq.com'];
$stmt->bindValue(1, $name, PDO::PARAM_STR);
$stmt->bindValue(2, $sex, PDO::PARAM_INT);
$stmt->bindValue(3, $email, PDO::PARAM_STR);
// 执行
$stmt->execute();
echo "参数绑定--值绑定: 新增了" . $stmt->rowCount() . "条记录";
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 参数绑定--引用绑定
$sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $name, PDO::PARAM_STR);
$stmt->bindParam(2, $sex, PDO::PARAM_INT);
$stmt->bindParam(3, $email, PDO::PARAM_STR);
list($name, $sex, $email) = ['小蒙', 0, 'dbc@qq.com'];
// 执行
$stmt->execute();
echo "参数绑定--引用绑定: 新增了" . $stmt->rowCount() . "条记录";
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 一次性增加多条
$sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $name, PDO::PARAM_STR);
$stmt->bindParam(2, $sex, PDO::PARAM_INT);
$stmt->bindParam(3, $email, PDO::PARAM_STR);
$data = [
['小王', 0, 'fbd@qq.com'],
['小五', 1, 'sbb@qq.com'],
['小平', 1, 'ebq@qq.com'],
['小蓝', 0, 'rbk@qq.com'],
];
// 执行
foreach ($data as list($name, $sex, $email)) {
$stmt->execute();
echo "一次性增加多条: 新增了" . $stmt->rowCount() . "条记录<br>";
}
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 更新
$sql = 'UPDATE `staff` SET `name`= ?,`sex`= ?,`email`= ? WHERE `id`=?';
if (false === stripos($sql, 'where')) {
exit('禁止无条件更新');
}
$stmt = $db->prepare($sql);
$data = ['小猪', 0, 'obd@qq.com', 5];
// 执行
if ($stmt->execute($data)) {
if ($stmt->rowCount() > 0) {
echo "成功更新了" . $stmt->rowCount() . "条数据!";
} else {
echo "没有记录被更新";
}
} else {
echo "执行失败";
}
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 删除
$sql = 'DELETE FROM `staff` WHERE `id`=?';
if (false === stripos($sql, 'where')) {
exit('禁止无条件更新');
}
$stmt = $db->prepare($sql);
// $data = [4];
// 执行
if ($stmt->execute([4])) {
if ($stmt->rowCount() > 0) {
echo "成功删除了" . $stmt->rowCount() . "条数据!";
} else {
echo "没有删除数据";
}
} else {
echo "执行失败";
}
<?php
namespace pdo;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
// 查询
$sql = 'SELECT * FROM `staff` LIMIT ?';
$stmt = $db->prepare($sql);
$stmt->bindValue(1, 2, PDO::PARAM_INT);
// 执行
echo "<br>-----------------------------while查询-----------------------------";
if ($stmt->execute()) {
while ($sta = $stmt->fetch(PDO::FETCH_ASSOC)) {
printf('<pre>%s</pre>', print_r($sta, true));
}
} else {
echo "执行失败";
}
echo "<br>-----------------------------foreach查询-----------------------------";
$stmt->bindValue(1, 3, PDO::PARAM_INT);
if ($stmt->execute()) {
$sta_all = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($sta_all as $sta_a) {
printf('<pre>%s</pre>', print_r($sta_a, true));
}
} else {
echo "执行失败";
}