亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP連接MySQL數(shù)據(jù)庫三種實(shí)現(xiàn)方法

 更新時(shí)間:2020年12月10日 09:54:50   作者:絕技小嗨皮  
這篇文章主要介紹了PHP連接MySQL數(shù)據(jù)庫三種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

引言

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ò)展提供了一個面向過程的接口,并且是針對MySQL4.1.3或者更早版本設(shè)計(jì)的。因此這個擴(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)用中的一個數(shù)據(jù)庫抽象層規(guī)范。PDO提供了一個統(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 起已廢棄,并在將來會被移除),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());//診斷連接錯誤
}
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);//診斷連接錯誤
}
$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)建一個pdo對象
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // 設(shè)置sql語句查詢?nèi)绻霈F(xiàn)問題 就會拋出異常
  //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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論