批改状态:合格
老师批语:看来你是真的理解了

<?php
namespace Article;
// 数据库查询类
class Query
{
// 先定义一些必须的属性——连接对象
public $pdo = null;
// 表名
public $table;
// 字段
public $field = '*';
// 条件
public $where;
// 数量
public $limit;
// 构造方法——这里必须
public function __construct($pdo){
// 实例时自动连接数据库
$this->pdo = $pdo;
}
// 设置数据表名称
public function table($tableName) {
$this->table = $tableName;
//返回当前类实例, 用来链式调用后面的其它方法
return $this;
}
// 设置数据表字段
public function field($fields = '*'){
$this->field = empty($fields) ? '*' : $fields;
return $this;
}
// 设置查询条件
public function where($where = ''){
$this->where = empty($where) ? $where : ' WHERE '. $where;
return $this;
}
// 设置显示数量
public function limit($limit){
$this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
return $this;
}
// 生成选择的SQL语句
public function select(){
// 拼装SQL
$sql = 'SELECT '
. $this->field // 字段列表
. ' FROM '
. $this->table // 数据表
. $this->where // 条件
. $this->limit; // 显示数量
// 预处理
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}点击 "运行实例" 按钮查看在线实例
继续上article.php文件
<?php
namespace Article;
require 'Query.php';
class DB{
/*
*先定义一个静态属性——连接对象
*这里定义个静态属性,并且还把的值定义为null,就是方便后面好赋值,好调用
*/
protected static $pdo = null;
/*
*然后定义一个静态方法——数据库的连接方法
*注意这里开始调用全局空间的PDO类,传入4个参数并实例化对象:数据库主机host的名称,数据库名称,用户名,登录密码
*/
public static function connection(){
self::$pdo = new \PDO('mysql:host=localhost;dbname=phpshouce', 'root', 'root');
}
/*
*继续定义一个魔术方法——静态方法,用来执行方法不存在时的回调
*/
public static function __callStatic($name, $arguments)
{
// 方法不存在,就执行前面的connection()连接数据库
self::connection();
// 实例化查询类,将连接对象做为参数
$query = new Query(self::$pdo);
// 调用查询对象$query中的对应的方法
return call_user_func_array([$query, $name],$arguments);
}
}
$info = DB::table('fubao_user')
->field('id,username,nickname,status')
->where('id > 2')
->limit(5)
->select();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>数据库操作</title>
</head>
<style>
/*给表格加上边框*/
table {
border: 1px solid #444444;
border-collapse: collapse;
width: 800px;
margin: 20px auto;
}
th,
td {
border: 1px solid #444444;
text-align: center;
padding: 10px;
}
table caption {
font-size: 1.3rem;
/*文本加粗*/
font-weight: bolder;
margin-bottom: 15px;
}
/* 第一行 */
#table-head-tr {
background-color: lightgreen;
}
/* 第一行 */
table thead>tr:first-of-type {
/* background-color: lightgreen; */
color: cornflowerblue;
}
table tbody>tr:first-of-type>td:first-of-type {
background-color: wheat;
}
table tbody>tr:nth-last-of-type(1)>td:first-of-type {
background-color: crimson;
color: darkorange;
}
/*圆角表格样式*/
/* 第一行左 */
table tr:first-child th:first-child {
border-top-left-radius: 12px;
}
/* 第一行右 */
table tr:first-child th:last-child {
border-top-right-radius: 12px;
}
</style>
<body>
<table>
<caption>数据库输出select</caption>
<thead>
<tr>
<th>ID</th>
<th>username</th>
<th>nickname</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
foreach($info as $data){
?>
<tr>
<td><?php echo $data['id']; ?></td>
<td><?php echo $data['username']; ?></td>
<td><?php echo $data['nickname']; ?></td>
<td><?php echo $data['status']; ?></td>
</tr>
<?php } ?>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号