ezSQL PHP數(shù)據(jù)庫操作類庫
更新時間:2010年05月16日 23:05:37 作者:
WordPress 使用的數(shù)據(jù)庫操作類就是它 -- ezSQL
我用了好多年了,我特別喜歡它的幾個類方法,可以有效提高代碼簡潔度。
ezSQL 下載地址:
下載 : ezSQL
新版本是2.05添加了很多支持,包括 CodeIgniter,MSSQL, PDO 等等
我之前也為 CodeIgniter 寫過一次,不過只支持 MySQL
看看使用示例
其實也沒什么難度,直接看源代碼即可,主要是程序設計的思想很好。
Example 1
----------------------------------------------------
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user ) {
// Access data using object syntax
echo $user->name;
echo $user->email;
}
Example 2
----------------------------------------------------
// Get one row from the database and print it out..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;
Example 3
----------------------------------------------------
// Get one variable from the database and print it out..
$var = $db->get_var("SELECT count(*) FROM users");
echo $var;
Example 4
----------------------------------------------------
// Insert into the database
$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");
Example 5
----------------------------------------------------
// Update the database
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");
Example 6
----------------------------------------------------
// Display last query and all associated results
$db->debug();
Example 7
----------------------------------------------------
// Display the structure and contents of any result(s) .. or any variable
$results = $db->get_results("SELECT name, email FROM users");
$db->vardump($results);
Example 8
----------------------------------------------------
// Get 'one column' (based on column index) and print it out..
$names = $db->get_col("SELECT name,email FROM users",0)
foreach ( $names as $name ) {
echo $name;
}
Example 9
----------------------------------------------------
// Same as above ‘but quicker'
foreach ( $db->get_col("SELECT name,email FROM users",0) as $name ) {
echo $name;
}
Example 10
----------------------------------------------------
// Map out the full schema of any given database and print it out..
$db->select("my_database");
foreach ( $db->get_col("SHOW TABLES",0) as $table_name ) {
$db->debug();
$db->get_results("DESC $table_name");
}
$db->debug();
EZSQL類介紹:
ezsql是一個小型的快速的數(shù)據(jù)庫操作類,可以讓你很容易地用PHP操作各種數(shù)據(jù)庫( MySQL、oracle8/9 、interbase、FireBird、PostgreSQL、MS-SQL、sqlite、sqlite C++)。
在你的腳本開頭是要包含一個一個PHP文件。然后,你就可以使用更小、更容易的一套ezsql函數(shù)來代替標準的PHP數(shù)據(jù)庫函數(shù)。
它會自動緩存的查詢結果,提供了一系列簡單的函數(shù)操作及擴展,并且沒有造成額外的服務器開銷
它具有優(yōu)良的調(diào)試功能,使你快速的判斷SQL語句的執(zhí)行過程
ezsql函數(shù)可以返回的結果是對象,關聯(lián)數(shù)組,或數(shù)值數(shù)組
它可以大大縮短開發(fā)時間,并在大多數(shù)情況下,將簡化您的代碼,讓其跑得更快,以及很容易調(diào)試和優(yōu)化您的數(shù)據(jù)庫查詢語句。
這是一個小類,在你的網(wǎng)站上并不會增加很大的開銷。
類中有以下的方法:
- $db->get_results – 從數(shù)據(jù)庫中讀取數(shù)據(jù)集 (or 之前緩存的數(shù)據(jù)集)
- $db->get_row — 從數(shù)據(jù)庫中讀取一條數(shù)據(jù) (or 之前緩存的數(shù)據(jù))
- $db->get_col – 從數(shù)據(jù)庫中讀取一列指定數(shù)據(jù)集 (or 之前緩存的數(shù)據(jù)集)
- $db->get_var — 從數(shù)據(jù)庫數(shù)據(jù)集中讀取一個值 (or 之前緩存的數(shù)據(jù))
- $db->query — 執(zhí)行一條sql語句(如果有數(shù)據(jù),就緩存起來)
- $db->debug – 打印最后執(zhí)行的sql語句與返回的結果(如果有結果)
- $db->vardump – 打印變量的結構及內(nèi)容
- $db->select — 選擇一個新數(shù)據(jù)庫
- $db->get_col_info – 獲取列的信息
- $db->donation – 捐錢給作者用的
- $db->escape – 格式化插入數(shù)據(jù)庫的字符串,eg:mysql_escape_string(stripslashes($str))
- $db->flush – 清除緩存
- $db->get_cache – 換取緩存
- $db->hide_errors – 隱藏錯誤
- $db->register_error – 注冊錯誤
- $db->show_errors – 顯示錯誤
- $db->store_cache – 存儲到緩存
- $db->sysdate – 獲取系統(tǒng)時間
- $db = new db — 建立一個新db對象.
wordpress對ezsql進行了修改,同時也使其僅適用于mysql
wordpress修改后的一些類操作也就是函數(shù)如下:
function query($query)
這個函數(shù)是 WPDB 最基本的函數(shù),$query 為 SQL 語句,提交給數(shù)據(jù)庫查詢,結果分二種情況:
1. 如果是 “insert|delete|update|replace”, 返回受影響行數(shù),在 “insert|replace”的情況下,用 $this->insert_id 記錄新插入的ID。
2. 如果是 “select”,用 $this->last_result 記下查詢結果集,返回查詢到的記錄行數(shù)。
function escape($string)
使用反斜線引用字符串,即使用魔術引號。
function insert($table, $data)
這是插入記錄函數(shù),第一個參數(shù)是表的字段數(shù)組,第二個是數(shù)據(jù)數(shù)組。插入數(shù)據(jù)返回1,否則為0。
function update($table, $data, $where)
這是更新紀錄函數(shù),第一個參數(shù)是表的字段數(shù)組,第二個是數(shù)據(jù)數(shù)組,第三個是條件數(shù)組,它是一個 nane array。更新了為1,否則為0。
function get_var($query=null, $x = 0, $y = 0)
如果 $query 不為空,首先執(zhí)行查詢,然后返回第 X 列 Y 行的值。
function get_row($query = null, $output = OBJECT, $y = 0)
返回一行,$outpu 指定返回的類型,可以是 ARRAY_A,ARRAY_N 或者 OBJECT。$y 指定第幾行。
function get_col($query = null , $x = 0)
返回一列,$x 指定第幾列。
function get_results($query = null, $output = OBJECT)
返回查詢結果集,可以以 ARRAY_A,ARRAY_N 或者 OBJECT 三種方式返回。
function get_col_info($info_type = ‘name', $col_offset = -1)
返回字段信息。
其他還有一些函數(shù),這里不詳細講了。另外還有兩個全局變量,SAVEQUERIES 和 WP_DEBUG,第一個是,可以讓你把訪問頁面執(zhí)行的查詢把保存到 $this->queries 這個數(shù)組中,以后調(diào)試的時候使用,WP_DEBUG 則讓你把錯誤輸出。這兩個默認都沒有打開,你測試的時候可以在 wp_config.php 中將其開啟。
下載 : ezSQL
新版本是2.05添加了很多支持,包括 CodeIgniter,MSSQL, PDO 等等
我之前也為 CodeIgniter 寫過一次,不過只支持 MySQL
看看使用示例
其實也沒什么難度,直接看源代碼即可,主要是程序設計的思想很好。
Example 1
----------------------------------------------------
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user ) {
// Access data using object syntax
echo $user->name;
echo $user->email;
}
Example 2
----------------------------------------------------
// Get one row from the database and print it out..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;
Example 3
----------------------------------------------------
// Get one variable from the database and print it out..
$var = $db->get_var("SELECT count(*) FROM users");
echo $var;
Example 4
----------------------------------------------------
// Insert into the database
$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");
Example 5
----------------------------------------------------
// Update the database
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");
Example 6
----------------------------------------------------
// Display last query and all associated results
$db->debug();
Example 7
----------------------------------------------------
// Display the structure and contents of any result(s) .. or any variable
$results = $db->get_results("SELECT name, email FROM users");
$db->vardump($results);
Example 8
----------------------------------------------------
// Get 'one column' (based on column index) and print it out..
$names = $db->get_col("SELECT name,email FROM users",0)
foreach ( $names as $name ) {
echo $name;
}
Example 9
----------------------------------------------------
// Same as above ‘but quicker'
foreach ( $db->get_col("SELECT name,email FROM users",0) as $name ) {
echo $name;
}
Example 10
----------------------------------------------------
// Map out the full schema of any given database and print it out..
$db->select("my_database");
foreach ( $db->get_col("SHOW TABLES",0) as $table_name ) {
$db->debug();
$db->get_results("DESC $table_name");
}
$db->debug();
EZSQL類介紹:
ezsql是一個小型的快速的數(shù)據(jù)庫操作類,可以讓你很容易地用PHP操作各種數(shù)據(jù)庫( MySQL、oracle8/9 、interbase、FireBird、PostgreSQL、MS-SQL、sqlite、sqlite C++)。
在你的腳本開頭是要包含一個一個PHP文件。然后,你就可以使用更小、更容易的一套ezsql函數(shù)來代替標準的PHP數(shù)據(jù)庫函數(shù)。
它會自動緩存的查詢結果,提供了一系列簡單的函數(shù)操作及擴展,并且沒有造成額外的服務器開銷
它具有優(yōu)良的調(diào)試功能,使你快速的判斷SQL語句的執(zhí)行過程
ezsql函數(shù)可以返回的結果是對象,關聯(lián)數(shù)組,或數(shù)值數(shù)組
它可以大大縮短開發(fā)時間,并在大多數(shù)情況下,將簡化您的代碼,讓其跑得更快,以及很容易調(diào)試和優(yōu)化您的數(shù)據(jù)庫查詢語句。
這是一個小類,在你的網(wǎng)站上并不會增加很大的開銷。
類中有以下的方法:
- $db->get_results – 從數(shù)據(jù)庫中讀取數(shù)據(jù)集 (or 之前緩存的數(shù)據(jù)集)
- $db->get_row — 從數(shù)據(jù)庫中讀取一條數(shù)據(jù) (or 之前緩存的數(shù)據(jù))
- $db->get_col – 從數(shù)據(jù)庫中讀取一列指定數(shù)據(jù)集 (or 之前緩存的數(shù)據(jù)集)
- $db->get_var — 從數(shù)據(jù)庫數(shù)據(jù)集中讀取一個值 (or 之前緩存的數(shù)據(jù))
- $db->query — 執(zhí)行一條sql語句(如果有數(shù)據(jù),就緩存起來)
- $db->debug – 打印最后執(zhí)行的sql語句與返回的結果(如果有結果)
- $db->vardump – 打印變量的結構及內(nèi)容
- $db->select — 選擇一個新數(shù)據(jù)庫
- $db->get_col_info – 獲取列的信息
- $db->donation – 捐錢給作者用的
- $db->escape – 格式化插入數(shù)據(jù)庫的字符串,eg:mysql_escape_string(stripslashes($str))
- $db->flush – 清除緩存
- $db->get_cache – 換取緩存
- $db->hide_errors – 隱藏錯誤
- $db->register_error – 注冊錯誤
- $db->show_errors – 顯示錯誤
- $db->store_cache – 存儲到緩存
- $db->sysdate – 獲取系統(tǒng)時間
- $db = new db — 建立一個新db對象.
wordpress對ezsql進行了修改,同時也使其僅適用于mysql
wordpress修改后的一些類操作也就是函數(shù)如下:
function query($query)
這個函數(shù)是 WPDB 最基本的函數(shù),$query 為 SQL 語句,提交給數(shù)據(jù)庫查詢,結果分二種情況:
1. 如果是 “insert|delete|update|replace”, 返回受影響行數(shù),在 “insert|replace”的情況下,用 $this->insert_id 記錄新插入的ID。
2. 如果是 “select”,用 $this->last_result 記下查詢結果集,返回查詢到的記錄行數(shù)。
function escape($string)
使用反斜線引用字符串,即使用魔術引號。
function insert($table, $data)
這是插入記錄函數(shù),第一個參數(shù)是表的字段數(shù)組,第二個是數(shù)據(jù)數(shù)組。插入數(shù)據(jù)返回1,否則為0。
function update($table, $data, $where)
這是更新紀錄函數(shù),第一個參數(shù)是表的字段數(shù)組,第二個是數(shù)據(jù)數(shù)組,第三個是條件數(shù)組,它是一個 nane array。更新了為1,否則為0。
function get_var($query=null, $x = 0, $y = 0)
如果 $query 不為空,首先執(zhí)行查詢,然后返回第 X 列 Y 行的值。
function get_row($query = null, $output = OBJECT, $y = 0)
返回一行,$outpu 指定返回的類型,可以是 ARRAY_A,ARRAY_N 或者 OBJECT。$y 指定第幾行。
function get_col($query = null , $x = 0)
返回一列,$x 指定第幾列。
function get_results($query = null, $output = OBJECT)
返回查詢結果集,可以以 ARRAY_A,ARRAY_N 或者 OBJECT 三種方式返回。
function get_col_info($info_type = ‘name', $col_offset = -1)
返回字段信息。
其他還有一些函數(shù),這里不詳細講了。另外還有兩個全局變量,SAVEQUERIES 和 WP_DEBUG,第一個是,可以讓你把訪問頁面執(zhí)行的查詢把保存到 $this->queries 這個數(shù)組中,以后調(diào)試的時候使用,WP_DEBUG 則讓你把錯誤輸出。這兩個默認都沒有打開,你測試的時候可以在 wp_config.php 中將其開啟。
您可能感興趣的文章:
- 簡單的php數(shù)據(jù)庫操作類代碼(增,刪,改,查)
- php mysql數(shù)據(jù)庫操作類
- 全新的PDO數(shù)據(jù)庫操作類php版(僅適用Mysql)
- PHP數(shù)據(jù)庫操作之基于Mysqli的數(shù)據(jù)庫操作類庫
- php實現(xiàn)mysql數(shù)據(jù)庫操作類分享
- PHP實現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
- php下mysql數(shù)據(jù)庫操作類(改自discuz)
- PHP輕量級數(shù)據(jù)庫操作類Medoo增加、刪除、修改、查詢例子
- PHP實現(xiàn)的MongoDB數(shù)據(jù)庫操作類分享
- php簡單數(shù)據(jù)庫操作類的封裝
相關文章
解析如何通過PHP函數(shù)獲取當前運行的環(huán)境 來進行判斷執(zhí)行邏輯(小技巧)
本篇文章是對如何通過PHP函數(shù)獲取當前運行的環(huán)境 來進行判斷執(zhí)行邏輯的技巧進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】
這篇文章主要介紹了PHP mongodb操作類定義與用法,結合實例形式分析了php封裝的適合mongodb2.x和mongodb3.x版本MongoDB數(shù)據(jù)庫連接、增刪改查、錯誤處理等操作定義與使用方法,需要的朋友可以參考下2018-06-062020最新版 PhpStudy V8.1版本下載安裝使用詳解
這篇文章主要介紹了2020最新版 PhpStudy V8.1版本下載安裝使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10php實現(xiàn)將普通數(shù)據(jù)轉(zhuǎn)化為json數(shù)據(jù)
在日常的開發(fā)工作中免不了要寫接口,json格式文本的輸出是制作接口必須掌握的,所以本文主要為大家詳細介紹了php如何將普通數(shù)據(jù)轉(zhuǎn)化為json數(shù)據(jù),需要的可以參考下2023-11-11