Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油
<?php
// 数据库连接参数
$db = [
'type' => 'mysql',
'host' => 'localhost',
'dbname' => 'zy2019_11_21',
'username' => 'root',
'password' => '123123',
];
// 配置数据源DSN信息
$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
// 连接数据库
try {
$pdo = new PDO($dsn, $db['username'], $db['password']);
} catch (PDOException $e) {
die('Connection Failed: ' . $e->getMessage());
}
?>
<?php
include 'conn.php';
echo '查询1: fetch() + while()';
// 1. 创建SQL语句模板
$sql = 'SELECT * FROM `goods` WHERE `gdtypeid` = :gdtypeid';
// 2. 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 3. 将变量绑定到SQL语句模板的命名占位符上
$gdtypeid = 2;
$stmt->bindParam('gdtypeid', $gdtypeid, PDO::PARAM_INT);
// 4. 执行SQL操作
$stmt->execute();
// 遍历
while ($goods = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<pre>' . print_r($goods, true);
}
echo '<hr>';
echo '查询2: fetchAll()';
// 创建SQL语句模板
$sql = 'SELECT * FROM `gdtype`' ;
// 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 5. 执行SQL操作
$stmt->execute();
// 遍历, 返回二维数组
$gdtype = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($gdtype as $gdtype) {
echo '<pre>' . print_r($gdtype, true);
}
// 销毁pdo, 关闭连接
$pdo = null;
?>
<?php
include 'conn.php';
echo '插入数据' .'<hr>';
// 1. 创建SQL语句模板
// $sql = 'INSERT INTO `goods` SET `code`= :code, `name`=:name, `gdtypeid`=:gdtypeid, `unit`=:unit, `sprice`=:sprice, `price`=:price';
$sql = 'INSERT INTO `goods` (`code`,`name`,`gdtypeid`,`unit`,`sprice`,`price`) VALUES (:code,:name,:gdtypeid,:unit,:sprice,:price)';
// 2. 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 3. 将变量绑定到SQL语句模板的命名占位符上
$code = 'W001';
$stmt->bindParam('code', $code, PDO::PARAM_STR);
$name = '商品名称1';
$stmt->bindParam('name', $name, PDO::PARAM_STR);
$gdtypeid = 4;
$stmt->bindParam('gdtypeid', $gdtypeid, PDO::PARAM_INT);
$unit = '个';
$stmt->bindParam('unit', $unit, PDO::PARAM_STR);
$sprice = 10.50;
$stmt->bindParam('sprice', $sprice, PDO::PARAM_STR);
$price = 5.50;
$stmt->bindParam('price', $price, PDO::PARAM_STR);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo '成功添加' . $stmt->rowCount(). '条记录, 该记录的主键id是: ' . $pdo->lastInsertId();
}
} else {
die('<pre>' . print_r($stmt->errorInfo(), true));
}
// 销毁pdo, 关闭连接
$pdo = null;
?>
<?php
include 'conn.php';
echo '更新数据' .'<hr>';
// 1. 创建SQL语句模板
$sql = 'UPDATE `goods` SET `name` = :name WHERE `goodsid`=:goodsid';
// 2. 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 3. 将变量绑定到SQL语句模板的命名占位符上
$goodsid =32;
$stmt->bindParam('goodsid', $goodsid, PDO::PARAM_INT);
$name = '商品名称修改';
$stmt->bindParam('name', $name, PDO::PARAM_STR);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo '成功修改' . $stmt->rowCount(). '条记录';
}
} else {
die('<pre>' . print_r($stmt->errorInfo(), true));
}
// 销毁pdo, 关闭连接
$pdo = null;
?>
<?php
include 'conn.php';
echo '删除数据' .'<hr>';
// 1. 创建SQL语句模板
$sql = 'DELETE FROM `goods` WHERE `goodsid`=:goodsid';
// 2. 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 3. 将变量绑定到SQL语句模板的命名占位符上
$goodsid =31;
$stmt->bindParam('goodsid', $goodsid, PDO::PARAM_INT);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo '成功删除' . $stmt->rowCount(). '条记录';
}
} else {
die('<pre>' . print_r($stmt->errorInfo(), true));
}
// 销毁pdo, 关闭连接
$pdo = null;
?>