##讓你快速使用PDO對MySQL做溝通
我的部落格Jsnpdo 影片:youtube 範例教學
Changelog 點我查看###說明書
id 他們得到同樣的值。
// 為什麼要用 ArrayObject ?
// 您不覺得打字 $DataInfo->id 手指頭快多了嗎?
/** 連接資料庫 **/
Jsnpdo::connect("mysql", "localhost", "資料庫名稱", "使用者帳號", "密碼");
/** 多筆查詢 原名select() **/
$DataList = Jsnpdo::sel("欄位", "資料表名稱", "其他條件"); // 無資料顯示0 | ArrayObject
$DataList = Jsnpdo::sel("欄位", "資料表名稱", "其他條件", 1); // debug 字串、中斷
$DataList = Jsnpdo::sel("欄位", "資料表名稱", "其他條件", str); // debug 字串、查詢顯示、中斷
$DataList = Jsnpdo::sel("欄位", "資料表名稱", "其他條件", chk); // debug 字串、查詢顯示、不中斷
//ex.
$w->id = Jsnpdo::quo($_POST['id']);
$w->title = Jsnpdo::quo($_POST['title']);
$DataList = Jsnpdo::sel("*", "jsntable", "where id = $w->id and title = $w->title");
$w = NULL;
if ($DataLsit != 0) foreach ($DataList as $DataInfo)
{
echo $DataInfo->id;
echo $DataInfo->title;
echo $DataInfo->content;
}
$DataList = Jsnpdo::sel("欄位", "資料表名稱", "其他條件");
/** 強制指定單筆查詢, 方法如同 sel()。原名 select_one() **/
$DataInfo = Jsnpdo::selone("欄位", "資料表名稱", "其他條件");
$DataInfo = Jsnpdo::selone("欄位", "資料表名稱", "其他條件", 1);
$DataInfo = Jsnpdo::selone("欄位", "資料表名稱", "其他條件", str);
$DataInfo = Jsnpdo::selone("欄位", "資料表名稱", "其他條件", chk);
if ($DataInfo != 0) echo $DataInfo->id;
/** 查詢快取 **/
Jsnpdo::$cache_life = 3; // 快取存活秒數
Jsnpdo::cache(true); // 開始快取
$DataList = Jsnpdo::selone("count(id) as `num_1`", "jsntable", ""); // 查詢
Jsnpdo::cache(false); // 停止快取
/** 利用陣列新增。原名 isnert() **/
$ary['id'] = NULL; // NULL 將自動調用 Jsnpdo::quo($_POST['id']);
$ary['title'] = Jsndpo::quo("標題"); // 自動添加 '' 並自動解決 sql injection 攻擊
$ary['content'] = Jsndpo::quo("sql injection"); // 自動添加 '' 並自動解決 sql injection 攻擊
$result = Jsnpdo::iary("資料表名稱", $ary, "POST或GET"); // 正常執行
$result = Jsnpdo::iary("資料表名稱", $ary, "POST或GET", 1); // debug 字串、中斷
$result = Jsnpdo::iary("資料表名稱", $ary, "POS或GETT", str); // debug 字串、事先查詢顯示、中斷
/** 利用陣列修改,原名 update() **/
$w->id = Jsnpdo::quo(100);
$w->owner = Jsnpdo::quo("張先生");
$ary['id'] = NULL; // NULL 將自動調用 Jsnpdo::quo($_POST['id']);
$ary['title'] = Jsndpo::quo("標題"); // 自動添加 '' 並自動解決 sql injection 攻擊
$ary['content'] = Jsndpo::quo("sql injection"); // 自動添加 '' 並自動解決 sql injection 攻擊
$result = Jsnpdo::uary("資料表名稱", $ary, "POST或GET", "where id = $w->id and owner = $w->owner"); // 執行
$result = Jsnpdo::uary("資料表名稱", $ary, "POST或GET", "條件", 1); // debug 字串、中斷
$result = Jsnpdo::uary("資料表名稱", $ary, "POST或GET", "條件", str); // debug 字串、事先查詢顯示、中斷
/** 刪除 **/
$w->id = Jsnpdo::quo(1);
// 限制 delete 已經自訂 where 條件,避免誤刪整個資料庫。仍要使用就使用 1 = 1
$result = Jsnpdo::delete("資料表名稱", "1 = 1");
$result = Jsnpdo::delete("資料表名稱", "1 = 1", 1); // debug 字串、中斷
$result = Jsnpdo::delete("資料表名稱", "and id = $w->id", str); // debug 字串、事先查詢顯示、中斷
/** 清空 **/
$result = Jsnpdo::truncate("資料表名稱");
/** 強大的過濾並自動添加 '' **/
Jsnpdo::quo(); //等於PHP的 PDO::quote()
/** 除錯的CSS樣式 '' **/
Jsnpdo::$debug_style = "position";
/** 在任何的query指令,不運行,而返回你下的 SQL 指令。預設false **/
Jsnpdo::$get_string = 1; // true:回傳SQL字串並終止query false: 不回傳,並運行query
/** PDO 的 query(); 執行SQL 方法 **/
Jsnpdo::query($sql, $status_debug); // $status_debug 有 1 或 NULL。 1 啟用debug 字串、中斷
/** 查詢後顯示剛取得的數量 **/
Jsnpdo::$select_num;
/** 快取存活時間**/
Jsnpdo::$cache_life = 3;
/** 開始或關閉快取**/
Jsnpdo::cache(true | false);
/** 得知目前查詢的指令,是設定快取(set)?還是讀取快取(get)?**/
Jsnpdo::cache_set_get();
?>
//查詢並自動解決 sql injection
$w->id = Jsnpdo::quo(1);
$DataInfo = Jsnpdo::selone("*", "jsntable", "where id = $w->id");
//新增
$_POST['title'] = "經由POST的標題";
$ary['id'] = NULL;
$ary['title'] = NULL;
$result = Jsnpdo::iary("jsntable", $ary, "POST");
//修改
$_POST['content'] = "經由POST的內容" . time();
$w->id = Jsnpdo::quo(1);
$ary['title'] = "更動標題";
$ary['content'] = NULL;
$result = Jsnpdo::uary("jsntable", $ary, "where id = $w->id", "POST");
或一般的實體物件寫法
$ary['title'] = NULL;
$result = $j->iary("jsntable", $ary, "POST");
若查詢需要快取
Jsnpdo::$cache_life = 3; //快取存活時間
Jsnpdo::cache(true); //開始快取
$DataList = Jsnpdo::selone("count(id) as `num_1`", "jsntable", "");
Jsnpdo::cache(false); //停止快取
###使用方法
- 前往 jsnpdo/jsnpdo/Demo.php
- include_once 對應你的所有路徑設定
- 設定你的資料庫資料Jsnpdo::connect("mysql", "localhost", "ci_jsn", "root", "");
- 重新整理就會看到極簡的單元測試
資料表建立成功
新增了1筆資料
新增了1筆資料
新增了1筆資料
最後新增的編號是:3
一次多筆執行成功
查詢多筆列表、取得總數成功
查詢單筆資料成功
取得查詢快取的狀態成功:get
查詢快取成功
修改1筆成功
刪除1筆成功
清空成功
刪除資料表成功
傳統寫法
新增成功
查詢成功
查詢單筆成功
修改成功
刪除成功
一次多筆新增成功
若使用快取,可了解目前的快取狀態是:get
快取查詢成功
若使用快取,可了解目前的快取狀態是:get
快取查詢成功
清空成功
刪除資料表成功