国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

pdo連接數據類與中文亂碼解決方法

2024-05-04 21:59:01
字體:
來源:轉載
供稿:網友

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服務器即可.

  1. extension=php_pdo.dll  
  2. extension=php_pdo_mysql.dll  
  3. extension=php_pdo_pgsql.dll  
  4. extension=php_pdo_sqlite.dll  
  5. extension=php_pdo_mssql.dll  
  6. extension=php_pdo_odbc.dll  
  7. extension=php_pdo_firebird.dll  
  8. ...... 

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數據庫實例,代碼如下:

  1. <?php  
  2. $pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");  
  3. if($pdo -> exec("insert into db_demo(name,content) values('title','content')")){  
  4. echo "插入成功!";  
  5. echo $pdo -> lastinsertid();  
  6. }  
  7. ?> 
  8.  
  9. <?php  
  10. $pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");  
  11. $rs = $pdo -> query("select * from test");  
  12. while($row = $rs -> fetch()){  
  13. print_r($row);  
  14. }  
  15. ?> 

網上最常出現的解決中文亂碼顯示的代碼是:

第一種: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,代碼如下:

  1. $pdo->exec("set character set gbk");  
  2. <?php  
  3. /* 
  4.  
  5. 常用數據庫操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數等  
  6. */  
  7. /*  
  8. 參數說明  
  9. int $debug 是否開啟調試,開啟則輸出sql語句  
  10. int $getcount 是否記數,返回值為行數  
  11. int $getrow 是否返回值單條記錄  
  12. string $table 數據庫表  
  13. string $fields 需要查詢的數據庫字段,允許為空,默認為查找全部  
  14. string $sqlwhere 查詢條件,允許為空  
  15. string $orderby 排序,允許為空,默認為id倒序  
  16. */ 
  17.  
  18. function hrselect($debug, $getcount, $getrow, $table, $fields="*", $sqlwhere="", $orderby="id desc"){  
  19. global $pdo;  
  20. if($debug){  
  21. if($getcount){  
  22. echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";  
  23. }else{  
  24. echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";  
  25. }  
  26. exit;  
  27. }else{  
  28. if($getcount){  
  29. $rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");  
  30. return $rs->fetchcolumn();  
  31. }elseif($getrow){  
  32. $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");  
  33. return $rs->fetch();  
  34. }else{  
  35. $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");  
  36. return $rs->fetchall();  
  37. }  
  38. }  
  39. }  
  40. /*  
  41. 參數說明  
  42. int $debug 是否開啟調試,開啟則輸出sql語句  
  43. int $execrow 是否開啟返回執行條目數  
  44. int $lastinsertid 是否開啟返回最后一條插入記錄id  
  45. string $table 數據庫表  
  46. string $fields 需要插入數據庫的字段  
  47. string $values 需要插入數據庫的信息,必須與$fields一一對應  
  48. */  
  49. function hrinsert($debug, $execrow, $lastinsertid, $table, $fields, $values){  
  50. global $pdo;  
  51. if($debug){  
  52. echo "insert into $table ($fields) values ($values)";  
  53. exit;  
  54. }elseif($execrow){  
  55. return $pdo->exec("insert into $table ($fields) values ($values)");  
  56. }elseif($lastinsertid){  
  57. return $pdo->lastinsertid("insert into $table ($fields) values ($values)");  
  58. }else{  
  59. $pdo->query("insert into $table ($fields) values ($values)");  
  60. }  
  61. }  
  62. /*  
  63. 參數說明  
  64. int $debug 是否開啟調試,開啟則輸出sql語句  
  65. int $execrow 是否開啟執行并返回條目數  
  66. string $table 數據庫表  
  67. string $set 需要更新的字段及內容,格式:a='abc',b=2,c='2010-10-10 10:10:10'  
  68. string $sqlwhere 修改條件,允許為空  
  69. */  
  70. function hrupdate($debug, $execrow, $table, $set, $sqlwhere=""){  
  71. global $pdo;  
  72. if($debug){  
  73. echo "update $table set $set where 1=1 $sqlwhere";  
  74. exit;  
  75. }elseif($execrow){  
  76. return $pdo->exec("update $table set $set where 1=1 $sqlwhere");  
  77. }else{  
  78. $pdo->query("update $table set $set where 1=1 $sqlwhere");  
  79. }  
  80. }  
  81. /*  
  82. 參數說明  
  83. int $debug 是否開啟調試,開啟則輸出sql語句  
  84. int $execrow 是否開啟返回執行條目數  
  85. string $table 數據庫表  
  86. string $sqlwhere 刪除條件,允許為空  
  87. */  
  88. function hrdelete($debug, $execrow, $table, $sqlwhere=""){  
  89. global $pdo;  
  90. if($debug){  
  91. echo "delete from $table where 1=1 $sqlwhere";  
  92. exit; //開源代碼Vevb.com 
  93. }elseif($execrow){  
  94. return $pdo->exec("delete from $table where 1=1 $sqlwhere");  
  95. }else{  
  96. $pdo->query("delete from $table where 1=1 $sqlwhere");  
  97. }  
  98. }  
  99. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 益阳市| 凭祥市| 延津县| 砀山县| 德钦县| 内乡县| 唐河县| 克山县| 明水县| 安泽县| 仙居县| 平邑县| 桦甸市| 厦门市| 宣化县| 长岛县| 图们市| 松滋市| 米脂县| 石狮市| 阳东县| 鱼台县| 苍溪县| 司法| 沂水县| 南江县| 民丰县| 突泉县| 中方县| 万宁市| 织金县| 和硕县| 中江县| 高州市| 当涂县| 邢台县| 谷城县| 当雄县| 黔东| 洞头县| 安乡县|