欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP連接MySQL數(shù)據(jù)庫的三種方式實例分析【mysql、mysqli、pdo】

 更新時間:2019年11月04日 09:56:06   作者:蒼青浪  
這篇文章主要介紹了PHP連接MySQL數(shù)據(jù)庫的三種方式,結合實例形式分析了PHP基于mysql、mysqli、pdo三種方式連接MySQL數(shù)據(jù)庫的相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了PHP連接MySQL數(shù)據(jù)庫的三種方式。分享給大家供大家參考,具體如下:

PHP與MySQL的連接有三種API接口,分別是:PHP的MySQL擴展 、PHP的mysqli擴展 、PHP數(shù)據(jù)對象(PDO) ,下面針對以上三種連接方式做下總結,以備在不同場景下選出最優(yōu)方案。

PHP的MySQL擴展是設計開發(fā)允許php應用與MySQL數(shù)據(jù)庫交互的早期擴展。MySQL擴展提供了一個面向過程的接口,并且是針對MySQL4.1.3或者更早版本設計的。因此這個擴展雖然可以與MySQL4.1.3或更新的數(shù)據(jù)庫服務端進行交互,但并不支持后期MySQL服務端提供的一些特性。由于太古老,又不安全,所以已被后來的mysqli完全取代;

PHP的mysqli擴展,我們有時稱之為MySQL增強擴展,可以用于使用 MySQL4.1.3或更新版本中新的高級特性。其特點為:面向?qū)ο蠼涌?、prepared語句支持、多語句執(zhí)行支持、事務支持 、增強的調(diào)試能力、嵌入式服務支持 、預處理方式完全解決了sql注入的問題。不過其也有缺點,就是只支持mysql數(shù)據(jù)庫。如果你要是不操作其他的數(shù)據(jù)庫,這無疑是最好的選擇。

PDO是PHP Data Objects的縮寫,是PHP應用中的一個數(shù)據(jù)庫抽象層規(guī)范。PDO提供了一個統(tǒng)一的API接口可以使得你的PHP應用不去關心具體要連接的數(shù)據(jù)庫服務器系統(tǒng)類型,也就是說,如果你使用PDO的API,可以在任何需要的時候無縫切換數(shù)據(jù)庫服務器,比如從Oracle 到MySQL,僅僅需要修改很少的PHP代碼。其功能類似于JDBC、ODBC、DBI之類接口。同樣,其也解決了sql注入問題,有很好的安全性。不過他也有缺點,某些多語句執(zhí)行查詢不支持(不過該情況很少)。

官文對于三者之間也做了列表性的比較:

  PHP的mysqli擴展 PDO PHP的mysql擴展
引入的PHP版本 5.0 5.0 3.0之前
PHP5.x是否包含 是  
MySQL開發(fā)狀態(tài) 活躍 在PHP5.3中活躍 僅維護
在MySQL新項目中的建議使用程度 建議 - 首選 建議 不建議
API的字符集支持
服務端prepare語句的支持情況
客戶端prepare語句的支持情況
存儲過程支持情況
多語句執(zhí)行支持情況 大多數(shù)
是否支持所有MySQL4.1以上功能 大多數(shù)
 

從官方給出的這份結果上來看,優(yōu)先推薦msqli,其次是PDO 。而“民間”給出的結果很多是傾向于使用PDO,因為其不擔有跨庫的優(yōu)點,更有讀寫速度快的特點。

1.PHP與Mysql擴展(本擴展自 PHP 5.5.0 起已廢棄,并在將來會被移除),PHP原生的方式去連接數(shù)據(jù)庫,是面向過程的

<?php
$mysql_conf = array(
  'host'  => '127.0.0.1:3306', 
  'db'   => 'test', 
  'db_user' => 'root', 
  'db_pwd' => 'root', 
  );
$mysql_conn = @mysql_connect($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if (!$mysql_conn) {
  die("could not connect to the database:\n" . mysql_error());//診斷連接錯誤
}
mysql_query("set names 'utf8'");//編碼轉(zhuǎn)化
$select_db = mysql_select_db($mysql_conf['db']);
if (!$select_db) {
  die("could not connect to the db:\n" . mysql_error());
}
$sql = "select * from user;";
$res = mysql_query($sql);
if (!$res) {
  die("could get the res:\n" . mysql_error());
}
while ($row = mysql_fetch_assoc($res)) {
  print_r($row);
}
mysql_close($mysql_conn);
?>

2.PHP與Mysqli擴展,面向過程、對象

<?php
$mysql_conf = array(
  'host'  => '127.0.0.1:3306', 
  'db'   => 'test', 
  'db_user' => 'root', 
  'db_pwd' => 'joshua317', 
  );
$mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if ($mysqli->connect_errno) {
  die("could not connect to the database:\n" . $mysqli->connect_error);//診斷連接錯誤
}
$mysqli->query("set names 'utf8';");//編碼轉(zhuǎn)化
$select_db = $mysqli->select_db($mysql_conf['db']);
if (!$select_db) {
  die("could not connect to the db:\n" . $mysqli->error);
}$sql = "select uid from user where name = 'joshua';";
$res = $mysqli->query($sql);
if (!$res) {
  die("sql error:\n" . $mysqli->error);
}
 while ($row = $res->fetch_assoc()) {
    var_dump($row);
  }
$res->free();
$mysqli->close();
?>

3.PHP與PDO擴展,面向過程、對象

<?php
$mysql_conf = array(
  'host'  => '127.0.0.1:3306', 
  'db'   => 'test', 
  'db_user' => 'root', 
  'db_pwd' => 'joshua317', 
  );
$pdo = new PDO("mysql:host=" . $mysql_conf['host'] . ";dbname=" . $mysql_conf['db'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);//創(chuàng)建一個pdo對象
$pdo->exec("set names 'utf8'");
$sql = "select * from user where name = ?";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, 'joshua', PDO::PARAM_STR);
$rs = $stmt->execute();
if ($rs) {
  // PDO::FETCH_ASSOC 關聯(lián)數(shù)組形式
  // PDO::FETCH_NUM 數(shù)字索引數(shù)組形式
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    var_dump($row);
  }
}
$pdo = null;//關閉連接
?>

更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論