1.pdo簡介
pdo(php data object) 是php 5 中加入的東西,是php 5新加入的一個重大功能,因為在php 5以前的php4/php3都是一堆的數據庫教程擴展來跟各個數據庫的連接和處理,什么 php_mysql教程.dll、php_pgsql.dll、php_mssql.dll、php_sqlite.dll等等。
php6中也將默認使用pdo的方式連接,mysql擴展將被作為輔助
2.pdo配置
php.ini中,去掉"extension=php_pdo.dll"前面的";"號,若要連接數據庫,還需要去掉與pdo相關的數據庫擴展前面的";"號,然后重啟apache服務器即可.
- extension=php_pdo.dll
- extension=php_pdo_mysql.dll
- extension=php_pdo_pgsql.dll
- extension=php_pdo_sqlite.dll
- extension=php_pdo_mssql.dll
- extension=php_pdo_odbc.dll
- extension=php_pdo_firebird.dll
- ......
3.pdo連接mysql數據庫
new pdo("mysql:host=localhost;dbname=db_demo","root","");
默認不是長連接,若要使用數據庫長連接,需要在最后加如下參數:
new pdo("mysql:host=localhost;dbname=db_demo","root","","array(pdo::attr_persistent => true) ");
4.pdo常用方法及其應用
pdo::query() 主要是用于有記錄結果返回的操作,特別是select操作
pdo::exec() 主要是針對沒有結果集合返回的操作,如insert、update等操作
pdo::lastinsertid() 返回上次插入操作,主鍵列類型是自增的最后的自增id
pdostatement::fetch() 是用來獲取一條記錄
pdostatement::fetchall() 是獲取所有記錄集到一個中
5.pdo操作mysql數據庫實例,代碼如下:
- <?php
- $pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
- if($pdo -> exec("insert into db_demo(name,content) values('title','content')")){
- echo "插入成功!";
- echo $pdo -> lastinsertid();
- }
- ?>
- <?php
- $pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
- $rs = $pdo -> query("select * from test");
- while($row = $rs -> fetch()){
- print_r($row);
- }
- ?>
網上最常出現的解決中文亂碼顯示的代碼是:
第一種:pdo::__construct($dsn, $user, $pass, array
(pdo::mysql_attr_init_command => "set names'utf8';"));
我試過用第一種方法,可結果是,name字段只顯示一個‘c'字符,之后的本該顯示中文的地方卻是空白,我是只要解決的:直接將utf8替換成了gbk,就可以了,即:
pdo::__construct($dsn, $user, $pass, array(pdo::mysql_attr_init_command => "set names'gbk';"));
第二種:pdo::__construct($dsn, $user, $pass);
pdo::exec("set names 'utf8';");
第二種我也在我的環境里測試過,碰到這種情況,把utf8替換成gbk,就能顯示了,另外,這里的pdo::在使用的時候用$pdo->代替,當然,這個是個變量,變量名稱可以自己定義.
第三種:$pdo->query('set names utf8;');
至于第三種呢,看了上面兩種,應該也知道要吧utf8替換成gbk,也能正確顯示了.
這幾種我都測試過了,都行,另外,我在這里還介紹一種解決中文亂碼的一種方法,不過大同小異,基本和第三種沒什么區別,不通的是,這種方法,沒用query而是用exec,代碼如下:
- $pdo->exec("set character set gbk");
- <?php
- /*
- 常用數據庫操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數等
- */
- /*
- 參數說明
- int $debug 是否開啟調試,開啟則輸出sql語句
- int $getcount 是否記數,返回值為行數
- int $getrow 是否返回值單條記錄
- string $table 數據庫表
- string $fields 需要查詢的數據庫字段,允許為空,默認為查找全部
- string $sqlwhere 查詢條件,允許為空
- string $orderby 排序,允許為空,默認為id倒序
- */
- function hrselect($debug, $getcount, $getrow, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
- global $pdo;
- if($debug){
- if($getcount){
- echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
- }else{
- echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
- }
- exit;
- }else{
- if($getcount){
- $rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
- return $rs->fetchcolumn();
- }elseif($getrow){
- $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
- return $rs->fetch();
- }else{
- $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
- return $rs->fetchall();
- }
- }
- }
- /*
- 參數說明
- int $debug 是否開啟調試,開啟則輸出sql語句
- int $execrow 是否開啟返回執行條目數
- int $lastinsertid 是否開啟返回最后一條插入記錄id
- string $table 數據庫表
- string $fields 需要插入數據庫的字段
- string $values 需要插入數據庫的信息,必須與$fields一一對應
- */
- function hrinsert($debug, $execrow, $lastinsertid, $table, $fields, $values){
- global $pdo;
- if($debug){
- echo "insert into $table ($fields) values ($values)";
- exit;
- }elseif($execrow){
- return $pdo->exec("insert into $table ($fields) values ($values)");
- }elseif($lastinsertid){
- return $pdo->lastinsertid("insert into $table ($fields) values ($values)");
- }else{
- $pdo->query("insert into $table ($fields) values ($values)");
- }
- }
- /*
- 參數說明
- int $debug 是否開啟調試,開啟則輸出sql語句
- int $execrow 是否開啟執行并返回條目數
- string $table 數據庫表
- string $set 需要更新的字段及內容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
- string $sqlwhere 修改條件,允許為空
- */
- function hrupdate($debug, $execrow, $table, $set, $sqlwhere=""){
- global $pdo;
- if($debug){
- echo "update $table set $set where 1=1 $sqlwhere";
- exit;
- }elseif($execrow){
- return $pdo->exec("update $table set $set where 1=1 $sqlwhere");
- }else{
- $pdo->query("update $table set $set where 1=1 $sqlwhere");
- }
- }
- /*
- 參數說明
- int $debug 是否開啟調試,開啟則輸出sql語句
- int $execrow 是否開啟返回執行條目數
- string $table 數據庫表
- string $sqlwhere 刪除條件,允許為空
- */
- function hrdelete($debug, $execrow, $table, $sqlwhere=""){
- global $pdo;
- if($debug){
- echo "delete from $table where 1=1 $sqlwhere";
- exit; //開源代碼Vevb.com
- }elseif($execrow){
- return $pdo->exec("delete from $table where 1=1 $sqlwhere");
- }else{
- $pdo->query("delete from $table where 1=1 $sqlwhere");
- }
- }
- ?>
新聞熱點
疑難解答