測試資料庫來源
其實應該第一期就交出的, 但現在提起也無礙
參考了安裝#mysql範例資料庫sakila
情境描述
我有一個用於測試的資料庫(sakila), 裡頭有一個表(actor), 現在我們將它和Model類別綁定就可以很輕鬆寫意地讀取資料了
首先, 新建一個類別, 類別名稱隨意, 但建議和表名一致
Actor .php
<?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']; }
發現了嗎?是不是非常的簡潔? 除開所有註解你甚至只需一行就設定完了
舉個例子
無需條件我就只是想讀取表裡的所有資料
<?php /** * 特别预告 * 想必有人发现需要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'; $a = Actor::get(); dd($a);
返回結果
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) 3 => object(Actor)[219] public 'actor_id' => string '4' (length=1) public 'first_name' => string 'JENNIFER' (length=8) public 'last_name' => string 'DAVIS' (length=5) public 'last_update' => string '2006-02-15 04:34:33' (length=19) // 更多数据......
可讀性更高且適用於當前要求的寫法
// 这回多一个小要求, 我只想看以下两个声明的字段 $a = Actor::all(['first_name', 'last_name']);
或
$a = 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) 3 => object(Actor)[219] public 'first_name' => string 'ALAN' (length=4) public 'last_name' => string 'DREYFUSS' (length=8) ......
-
我想搜尋first_name中包含L這個字母的資料, 但不要前5條資料, 取之後的10條資料, 想查看first_name和last_name這兩個欄位就足夠了
最具可讀性的寫法,
再預告, 分頁(paginate)的雛形就在這裡$a = Actor::select('first_name', 'last_name') ->where('first_name', 'like', '%L%') ->skip(5) // 略过5条 ->take(10) // 拿取10条 ->get();
返回結果
array (size=10) 0 => object(Actor)[20] public 'first_name' => string 'ANGELA' (length=6) public 'last_name' => string 'HUDSON' (length=6) 1 => object(Actor)[24] public 'first_name' => string 'ANGELA' (length=6) public 'last_name' => string 'WITHERSPOON' (length=11) 2 => object(Actor)[28] public 'first_name' => string 'ANGELINA' (length=8) public 'last_name' => string 'ASTAIRE' (length=7) 3 => object(Actor)[32] public 'first_name' => string 'BELA' (length=4) public 'last_name' => string 'WALKEN' (length=6) 4 => object(Actor)[36] public 'first_name' => string 'CHARLIZE' (length=8) public 'last_name' => string 'DENCH' (length=5) 5 => object(Actor)[40] public 'first_name' => string 'DARYL' (length=5) public 'last_name' => string 'CRAWFORD' (length=8) 6 => object(Actor)[44] public 'first_name' => string 'DARYL' (length=5) public 'last_name' => string 'WAHLBERG' (length=8) 7 => object(Actor)[48] public 'first_name' => string 'ELLEN' (length=5) public 'last_name' => string 'PRESLEY' (length=7) 8 => object(Actor)[52] public 'first_name' => string 'ELVIS' (length=5) public 'last_name' => string 'MARX' (length=4) 9 => object(Actor)[56] public 'first_name' => string 'EMILY' (length=5) public 'last_name' => string 'DEE' (length=3)
想要更多的例子?留言吧我會在評論區給出函數鏈
調試小技巧
有時候可能對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)
以上是如何寫一個屬於自己的資料庫封裝(4)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

MySQL適合初學者學習數據庫技能。 1.安裝MySQL服務器和客戶端工具。 2.理解基本SQL查詢,如SELECT。 3.掌握數據操作:創建表、插入、更新、刪除數據。 4.學習高級技巧:子查詢和窗口函數。 5.調試和優化:檢查語法、使用索引、避免SELECT*,並使用LIMIT。

MySQL通過表結構和SQL查詢高效管理結構化數據,並通過外鍵實現表間關係。 1.創建表時定義數據格式和類型。 2.使用外鍵建立表間關係。 3.通過索引和查詢優化提高性能。 4.定期備份和監控數據庫確保數據安全和性能優化。

MySQL是一個開源的關係型數據庫管理系統,廣泛應用於Web開發。它的關鍵特性包括:1.支持多種存儲引擎,如InnoDB和MyISAM,適用於不同場景;2.提供主從復制功能,利於負載均衡和數據備份;3.通過查詢優化和索引使用提高查詢效率。

SQL用於與MySQL數據庫交互,實現數據的增、刪、改、查及數據庫設計。 1)SQL通過SELECT、INSERT、UPDATE、DELETE語句進行數據操作;2)使用CREATE、ALTER、DROP語句進行數據庫設計和管理;3)複雜查詢和數據分析通過SQL實現,提升業務決策效率。

MySQL的基本操作包括創建數據庫、表格,及使用SQL進行數據的CRUD操作。 1.創建數據庫:CREATEDATABASEmy_first_db;2.創建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入數據:INSERTINTObooks(title,author,published_year)VA

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

構建MySQL數據庫的步驟包括:1.創建數據庫和表,2.插入數據,3.進行查詢。首先,使用CREATEDATABASE和CREATETABLE語句創建數據庫和表,然後用INSERTINTO語句插入數據,最後用SELECT語句查詢數據。

MySQL適合初學者,因為它易用且功能強大。 1.MySQL是關係型數據庫,使用SQL進行CRUD操作。 2.安裝簡單,需配置root用戶密碼。 3.使用INSERT、UPDATE、DELETE、SELECT進行數據操作。 4.複雜查詢可使用ORDERBY、WHERE和JOIN。 5.調試需檢查語法,使用EXPLAIN分析查詢。 6.優化建議包括使用索引、選擇合適數據類型和良好編程習慣。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具