
批改狀態(tài):合格
老師批語(yǔ):如果你是第一次接觸面向接口編程, 這種想法正常的, 能把這個(gè)案例寫(xiě)對(duì)很不容易
<?php
$config = require 'config.php';
//定義接口類(lèi)
interface iDB
{
// 增加
public static function insert($pdo, $data);
// 刪除
public static function delete($pdo, $where);
// 查詢
public static function select($pdo, $options = []);
// 修改
public static function update($pdo, $options);
}
//定義抽象類(lèi)實(shí)現(xiàn)接口,封裝公共數(shù)據(jù)庫(kù)連接方法
abstract class aDB implements iDB
{
protected static $pdo;
//數(shù)據(jù)庫(kù)連接方法
public static function conn($dsn, $user, $pwd)
{
//判斷數(shù)據(jù)庫(kù)連接對(duì)象是否存在
if (is_null(self::$pdo)) {
try {
$pdo = new PDO($dsn, $user, $pwd);
// 設(shè)置結(jié)果集的默認(rèn)獲取模式
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
} catch (PDOException $e) {
exit('Connection Error: ' . $e->getMessage());
}
}
return self::$pdo;
}
}
//實(shí)現(xiàn)類(lèi),實(shí)現(xiàn)接口中的方法
class DB extends aDB
{
// 增加
public static function insert($pdo, $data)
{
$sql = "INSERT INTO `users`(`name`,`pwd`,`age`,`tel`) VALUES (?,?,?,?)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $name, PDO::PARAM_STR, 50);
$stmt->bindParam(2, $pwd, PDO::PARAM_STR, 50);
$stmt->bindParam(3, $age, PDO::PARAM_INT, 30);
$stmt->bindParam(4, $tel, PDO::PARAM_STR, 50);
$num = 0;
foreach ($data as $user) {
extract($user);
$stmt->execute() or die(print_r($stmt->errorInfo(), true));
if ($stmt->rowCount() > 0) {
$num += $stmt->rowCount();
}
}
printf("成功添加 %s 條數(shù)據(jù)", $num);
}
// 刪除
public static function delete($pdo, $where)
{
$sql = "delete from `users` where `id` = ? ";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $where);
$stmt->execute() or die(print_r('刪除失敗' . $stmt->errorInfo(), true));
printf("成功刪除%s條數(shù)據(jù)", $stmt->rowCount());
}
// 查詢
public static function select($pdo, $options = [])
{
$sql = "select `id`,`name`,`age` from `users` where id>= ?";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1,$options, PDO::PARAM_INT, 30);
$stmt->execute() or die(print_r('查詢失敗' . $stmt->errorInfo()));
foreach ($stmt->fetchAll() as $user) {
vprintf("id:%s,name:%s,age:%s<br>", $user);
}
}
// 修改
public static function update($pdo, $options)
{
$sql = "update `users` set `name`=? where id=? ";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $options['name'], PDO::PARAM_STR, 50);
$stmt->bindParam(2, $options['id'], PDO::PARAM_INT, 20);
$stmt->execute() or die(print_r('更新數(shù)據(jù)失敗:' . $stmt->errorInfo()));
if ($stmt->rowCount() > 0):
print_r('成功更新' . $stmt->rowCount() . '條數(shù)據(jù)');
endif;
}
}
extract($config);
$dsn = "$type:host=$host;dbname=$dbname;charset=$charset;port=$port";
$pdo = DB::conn($dsn, $username, $password);
$users = [
['name' => 'Adalia1', 'age' => 33, 'pwd' => sha1('1256'), 'tel' => '188938801'],
['name' => 'Brenda1', 'age' => 12, 'pwd' => sha1('12456'), 'tel' => '178922801'],
['name' => 'Alice1', 'age' => 55, 'pwd' => sha1('12346'), 'tel' => '168448801'],
['name' => '騎??癖?', 'age' => 65, 'pwd' => sha1('13456'), 'tel' => '138966801'],
['name' => 'Catherine1', 'age' => 111, 'pwd' => sha1('23456'), 'tel' => '118988801'],
];
//增加
//DB::insert($pdo, $users);
//刪除
//DB::delete($pdo,110);
//查詢
//DB::select($pdo,88);
//更新
DB::update($pdo,['name'=>'趙四333','id'=>118]);
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)