pdo串連資料類與中文亂碼解決方案_PHP教程

來源:互聯網
上載者:User
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資料庫執行個體


複製代碼 代碼如下:

$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();
}
?>


複製代碼 代碼如下:

$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'字元。之後的本該顯示中文的地方卻是空白。

結果是這樣的:1示

我是只要解決的:直接將utf8替換成了gbk,就可以了,即:

pdo::__construct($dsn, $user, $pass, array(pdo::mysql_attr_init_command => "set

names'gbk';"));

2如下:


第二種:pdo::__construct($dsn, $user, $pass);

pdo::exec("set names 'utf8';");

第二種我也在我的環境裡測試過,顯示效果1所示,碰到這種情況,把utf8替換成gbk,就能顯

示了。另外,這裡的pdo::在使用的時候用$pdo->代替,當然,這個是個變數,變數名稱可以自己定義。

第三種

:$pdo->query('set names utf8;');

至於第三種呢,看了上面兩種,應該也知道要吧utf8替換成gbk,也能正確顯示了。

這幾種我都測試過了。都行。哈哈。另外,我在這裡還介紹一種解決中文亂碼的一種方法,不過大同小異,

基本和第三種沒什麼卻別,不通的是,這種方法,沒用query而是用exec,代碼如下:

$pdo->exec("set character set gbk");

/*

常用資料庫操作,如:增刪改查,擷取單條記錄、多條記錄,返回最新一條插入記錄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;
}elseif($execrow){
return $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
}
}
?>

http://www.bkjia.com/PHPjc/630758.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630758.htmlTechArticle1.pdo簡介 pdo(php教程 data object) 是php 5 中加入的東西,是php 5新加入的一個重大功能,因為在php 5以前的php4/php3都是一堆的資料庫教程擴充來跟...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.