1、Db.class.php
// 连接数据库
class Db {
static public function getDB() {
try {
$pdo = new PDO(DB_DSN, DB_USER, DB_PWD);
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // 设置数据库连接为持久连接
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置抛出错误
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true); // 设置当字符串为空转换为 SQL 的 NULL
$pdo->query('SET NAMES utf8'); // 设置数据库编码
} catch (PDOException $e) {
exit('数据库连接错误,错误信息:'. $e->getMessage());
}
return $pdo;
}
}
?>
// 连接数据库
class Db {
static public function getDB() {
try {
$pdo = new PDO(DB_DSN, DB_USER, DB_PWD);
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // 设置数据库连接为持久连接
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置抛出错误
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true); // 设置当字符串为空转换为 SQL 的 NULL
$pdo->query('SET NAMES utf8'); // 设置数据库编码
} catch (PDOException $e) {
exit('数据库连接错误,错误信息:'. $e->getMessage());
}
return $pdo;
}
}
?>
2、Model.class.php
// 操作 SQL
class Model {
/**
* SQL 增删改操作,返回受影响的行数
* @param string $sql
* @return int
*/
public function aud($sql) {
try {
$pdo = Db::getDB();
$row = $pdo->exec($sql);
} catch (PDOException $e) {
exit($e->getMessage());
}
return $row;
}
/**
* 返回全部数据,返回 PDOStatement 对象
* @param string $sql
* @return PDOStatement
*/
public function getAll($sql) {
try {
$pdo = Db::getDB();
$result = $pdo->query($sql);
return $result;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
?>
摘自 Lee.的专栏

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!