PHP連接MySQL數(shù)據(jù)庫三種實(shí)現(xiàn)方法
引言
PHP與MySQL的連接有三種API接口,分別是:PHP的MySQL擴(kuò)展 、PHP的mysqli擴(kuò)展 、PHP數(shù)據(jù)對象(PDO) ,下面針對以上三種連接方式做下總結(jié),以備在不同場景下選出最優(yōu)方案。
PHP的MySQL擴(kuò)展是設(shè)計(jì)開發(fā)允許php應(yīng)用與MySQL數(shù)據(jù)庫交互的早期擴(kuò)展。MySQL擴(kuò)展提供了一個(gè)面向過程的接口,并且是針對MySQL4.1.3或者更早版本設(shè)計(jì)的。因此這個(gè)擴(kuò)展雖然可以與MySQL4.1.3或更新的數(shù)據(jù)庫服務(wù)端進(jìn)行交互,但并不支持后期MySQL服務(wù)端提供的一些特性。由于太古老,又不安全,所以已被后來的mysqli完全取代;
PHP的mysqli擴(kuò)展,我們有時(shí)稱之為MySQL增強(qiáng)擴(kuò)展,可以用于使用 MySQL4.1.3或更新版本中新的高級特性。其特點(diǎn)為:面向?qū)ο蠼涌?、prepared語句支持、多語句執(zhí)行支持、事務(wù)支持 、增強(qiáng)的調(diào)試能力、嵌入式服務(wù)支持 、預(yù)處理方式完全解決了sql注入的問題。不過其也有缺點(diǎn),就是只支持mysql數(shù)據(jù)庫。如果你要是不操作其他的數(shù)據(jù)庫,這無疑是最好的選擇。
PDO是PHP Data Objects的縮寫,是PHP應(yīng)用中的一個(gè)數(shù)據(jù)庫抽象層規(guī)范。PDO提供了一個(gè)統(tǒng)一的API接口可以使得你的PHP應(yīng)用不去關(guān)心具體要連接的數(shù)據(jù)庫服務(wù)器系統(tǒng)類型,也就是說,如果你使用PDO的API,可以在任何需要的時(shí)候無縫切換數(shù)據(jù)庫服務(wù)器,比如從Oracle 到MySQL,僅僅需要修改很少的PHP代碼。其功能類似于JDBC、ODBC、DBI之類接口。同樣,其也解決了sql注入問題,有很好的安全性。不過他也有缺點(diǎn),某些多語句執(zhí)行查詢不支持(不過該情況很少)。
代碼示例
PHP與Mysql擴(kuò)展(本擴(kuò)展自 PHP 5.5.0 起已廢棄,并在將來會(huì)被移除),PHP原生的方式去連接數(shù)據(jù)庫,是面向過程的
$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());//診斷連接錯(cuò)誤}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);
PHP與Mysqli擴(kuò)展,面向過程、對象
<?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);//診斷連接錯(cuò)誤}$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();?>
PHP與PDO擴(kuò)展,面向過程、對象
<?php$mysql_conf = array( ’host’ => ’127.0.0.1:3306’, ’db’ => ’test’, ’db_user’ => ’root’, ’db_pwd’ => ’joshua317’, );try { $pdo = new PDO('mysql:host=' . $mysql_conf[’host’] . ';dbname=' . $mysql_conf[’db’], $mysql_conf[’db_user’], $mysql_conf[’db_pwd’]);//創(chuàng)建一個(gè)pdo對象 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 設(shè)置sql語句查詢?nèi)绻霈F(xiàn)問題 就會(huì)拋出異常 //set_exception_handler('cus_exception_handler');} catch (PDOException $e) { die('connect error:'.$e->getMessage());}$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 關(guān)聯(lián)數(shù)組形式 // PDO::FETCH_NUM 數(shù)字索引數(shù)組形式 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { var_dump($row); }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送3. Python編寫nmap掃描工具4. php5.6不能擴(kuò)展redis.so的解決方法5. python 爬取嗶哩嗶哩up主信息和投稿視頻6. 關(guān)于HTML5的img標(biāo)簽7. python 如何停止一個(gè)死循環(huán)的線程8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總
