CSS 变换(transform)详解
概述
本文综合演练 PHP MySQL 操作,通过完整 CRUD 示例巩固所学知识。
完整 CRUD 示例
class ArticleService {
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
// 创建
public function create($title, $content, $category) {
$stmt = $this->pdo->prepare(
"INSERT INTO articles (title, content, category, created_at) VALUES (?, ?, ?, NOW())"
);
$stmt->execute([$title, $content, $category]);
return $this->pdo->lastInsertId();
}
// 读取
public function find($id) {
$stmt = $this->pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
public function findAll($category = null) {
if ($category) {
$stmt = $this->pdo->prepare("SELECT * FROM articles WHERE category = ?");
$stmt->execute([$category]);
} else {
$stmt = $this->pdo->query("SELECT * FROM articles");
}
return $stmt->fetchAll();
}
// 更新
public function update($id, $title, $content) {
$stmt = $this->pdo->prepare(
"UPDATE articles SET title = ?, content = ?, updated_at = NOW() WHERE id = ?"
);
$stmt->execute([$title, $content, $id]);
return $stmt->rowCount();
}
// 删除
public function delete($id) {
$stmt = $this->pdo->prepare("DELETE FROM articles WHERE id = ?");
$stmt->execute([$id]);
return $stmt->rowCount();
}
}
总结
掌握完整的 CRUD 操作,就能完成大多数数据管理功能。再配合上错误处理和安全防护,就是合格的 PHP MySQL 代码。