php可以封装数据库连接4,如何写一个属于自己的数据库封装(4) - 查询 - 入门篇用法(修订版)...

测试数据库来源

其实应该第一期就交出的, 但现在提起也无碍

参考了安装mysql示例数据库sakila

情景描述

我有一个用于测试的数据库(sakila), 里头有一个表(actor), 现在我们将它和Model类绑定就可以很轻松写意地读取数据了

首先, 新建一个类, 类名随意, 但建议和表名一致

Actor.php

/**

* 数据库中的 Actor 表

* 继承 Model 的属性和函数

*/

class Actor extends Model {

// 由于我们的数据库表名和当前的类名是一样的,可以直接省略这一步

// protected $table = 'Actor';

// 设置 Actor 表的主键

protected $identity = 'actor_id';

// 或者设置 unique key

// 如果 unique key 只有一个

// protected $unique = 'actor_id';

// 如果多个 unique key

// protected $unique = ['first_name', 'last_name'];

}

发现了吗?是不是非常的简洁? 除开所有注释你甚至只需一行就设置完毕了

例子

特别预告

想必有人发现需要 require 的文件可能多了一些

这可以用自动载入来解决

敬请期待

先载入需求文件

// 辅助函数我都写 helper 里了

require 'lib/helper.php';

require 'lib/Connector.php';

require 'lib/Builder.php';

require 'lib/Grammar.php';

require 'lib/Model.php';

require 'model/Actor.php';

读取表里所有的数据

Actor::get();

或者

// 更可读的写法, 列出所有(all)的 Actor

Actor::all();

返回结果

array (size=197)

0 =>

object(Actor)[207]

public 'actor_id' => string '1' (length=1)

public 'first_name' => string 'PENELOPE' (length=8)

public 'last_name' => string 'GUINESS' (length=7)

public 'last_update' => string '2006-02-15 04:34:33' (length=19)

1 =>

object(Actor)[211]

public 'actor_id' => string '2' (length=1)

public 'first_name' => string 'NICK' (length=4)

public 'last_name' => string 'WAHLBERG' (length=8)

public 'last_update' => string '2006-02-15 04:34:33' (length=19)

2 =>

object(Actor)[215]

public 'actor_id' => string '3' (length=1)

public 'first_name' => string 'ED' (length=2)

public 'last_name' => string 'CHASE' (length=5)

public 'last_update' => string '2006-02-15 04:34:33' (length=19)

......

只查询指定的字段 ('first_name', 'last_name')

Actor::get(['first_name', 'last_name']);

或者

Actor::select('first_name', 'last_name')->get();

或者

Actor::select(['first_name', 'last_name'])->get();

返回结果

array (size=197)

0 =>

object(Actor)[207]

public 'first_name' => string 'ADAM' (length=4)

public 'last_name' => string 'GRANT' (length=5)

1 =>

object(Actor)[211]

public 'first_name' => string 'ADAM' (length=4)

public 'last_name' => string 'HOPPER' (length=6)

2 =>

object(Actor)[215]

public 'first_name' => string 'AL' (length=2)

public 'last_name' => string 'GARLAND' (length=7)

......

列出所有数据, 以 first_name 顺序排列, 但不要前5条数据, 取之后的10条数据, 查询first_name和last_name两个字段

最具可读性的写法,

再预告, 分页(paginate)的雏形就在这里

Actor::select('first_name', 'last_name')

->skip(5) // 略过5条

->take(10) // 拿取10条

->orderBy('first_name')

->get();

返回结果

array (size=10)

0 =>

object(Actor)[20]

public 'first_name' => string 'ALBERT' (length=6)

public 'last_name' => string 'NOLTE' (length=5)

1 =>

object(Actor)[24]

public 'first_name' => string 'ALEC' (length=4)

public 'last_name' => string 'WAYNE' (length=5)

2 =>

object(Actor)[28]

public 'first_name' => string 'ANGELA' (length=6)

public 'last_name' => string 'HUDSON' (length=6)

......

想要更多的例子?留言吧我会在评论区给出函数链

调试小技巧

有时候可能对 SQL 语句的不理解导致跳 BUG 了, 这时候你想看看原生的SQL语句, 可以在 Connector.php 中的 read() 函数处, 这样加上

// 读取数据

public function read($sql, $bindings) {

var_dump($sql); // 就是这一句

var_dump($bindings); // 还可以确认条件值是否正确对应

// 将sql语句放入预处理函数

$statement = $this->connection->prepare($sql);

// 将附带参数带入pdo实例

$this->bindValues($statement, $bindings);

// 执行

$statement->execute();

// 返回所有合法数据, 以Object对象为数据类型

return $statement->fetchAll(PDO::FETCH_OBJ);

}

以最后一个例子为说明, 会输出这么两行

string 'select first_name, last_name from Actor where Actor.first_name like ? order by 1 asc limit 10 offset 5' (length=102)

array (size=1)

0 => string '%L%' (length=3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值