生活随笔
收集整理的這篇文章主要介紹了
CI框架中pdo的使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、配置
application/config文件夾下的database.php文件
[php] view plaincopy
$active_group?=?'default';??$active_record?=?TRUE;??$db['default']['hostname']?=?'mysql:hostname=localhost;dbname=myproject';??$db['default']['username']?=?'myproject';??$db['default']['password']?=?'myproject';??$db['default']['database']?=?'';??$db['default']['dbdriver']?=?'pdo';??$db['default']['dbprefix']?=?'';??$db['default']['pconnect']?=?TRUE;??$db['default']['db_debug']?=?TRUE;??$db['default']['cache_on']?=?FALSE;??$db['default']['cachedir']?=?'';??$db['default']['char_set']?=?'utf8';??$db['default']['dbcollat']?=?'utf8_general_ci';??$db['default']['swap_pre']?=?'';??$db['default']['autoinit']?=?TRUE;??$db['default']['stricton']?=?FALSE;??
2、使用方法
手動加載數據庫
$this->load->database()
自動加載數據庫
application/config文件夾下的autoload.php文件
[php] view plaincopy
$autoload['libraries']?=?array('database');??
select :
[php] view plaincopy
$sql?=?'select?*?from?aaa?where?id?=?:id';??$sql_array?=?array(??????':id'?=>?1???);??$stmt?=?$this->db->conn_id->prepare($sql);??$stmt->execute($sql_array);??$arr_user?=?$stmt->fetchAll(PDO::FETCH_ASSOC);???$str_user?=?$stmt->fetch(PDO::FETCH_ASSOC);???
獲取查詢結果總行數兩種方法:(第二種更有效率)
[php] view plaincopy
(1)、$Count_num?=?count($arr_user);????(2)、??$sql?=?'select?*?from?aaa?where?id?=?:id';??$sql_array?=?array(??????':id'?=>?1???);??$stmt?=?$this->db->conn_id->prepare($sql);??$stmt->execute($sql_array);??$str_user?=?$stmt->fetch(PDO::FETCH_ASSOC);??$Count_num?=?$str_user[0];??
判斷查詢是否成功:
[php] view plaincopy
$stmt->execute($sql_array);???
insert 、update、delete :
除了sql語句語法不同,查詢的方法是一樣的
[php] view plaincopy
$sql?=?"update?aaa?SET?status?=?-1?WHERE?id=?:id";??$sql?=?"INSERT?INTO?aaa(`id`)?VALUES?(?:id)";??$sql?=?"delete?from?aaa?where?id=?:id";????$sql_array?=?array(??????':id'?=>?1???);??$stmt?=?$this->db->conn_id->prepare($sql);??$stmt->execute($sql_array);??
判斷查詢是否成功:
[php] view plaincopy
$stmt->rowCount();??if($stmt->rowCount()>0){?????}else{?????}??
另外,insert的時候,有時候需要返回insert_id
[php] view plaincopy
pdo中的寫法是?$stmt->lastInsertId();??
來源:http://blog.csdn.net/jalc2803/article/details/47258743
總結
以上是生活随笔為你收集整理的CI框架中pdo的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。