PHP與數(shù)據(jù)庫(kù)常用擴(kuò)展:mysqli & mysql
PHP與數(shù)據(jù)庫(kù)常用擴(kuò)展:mysqli & mysql"/>無(wú)論是哪種互聯(lián)網(wǎng)應(yīng)用,數(shù)據(jù)庫(kù)都是必不可少的一環(huán),本文將介紹php中數(shù)據(jù)庫(kù)的相關(guān)擴(kuò)展mysqli與mysql。mysqli是mysql擴(kuò)展的新版,支持事務(wù)處理和面向?qū)ο蟮恼Z(yǔ)言調(diào)用方式,用于mysql8.0 (及以后的版本),而mysql則是php早期的數(shù)據(jù)庫(kù)擴(kuò)展。
mysqli實(shí)現(xiàn)與mysql的區(qū)別
mysqli與mysql的用法上有所不同。mysqli支持面向?qū)ο蠛突谶^(guò)程,可以針對(duì)不同的用戶(hù)操作。在此,我們以面向?qū)ο蠛突谶^(guò)程各一個(gè)舉例:
$mysqli = new mysqli("localhost", "root", "", "example"); //面向?qū)ο? if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; } mysqli_query($mysqli,"CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )"); //向表中插入數(shù)據(jù) mysqli_query($mysqli,"INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (1,'Doe','John','456 Park Avenue','New York')"); mysqli_close($mysqli); //關(guān)閉連接 $mysqli = mysqli_connect("localhost","root","","example"); //基于過(guò)程 if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($mysqli,"CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )"); //向表中插入數(shù)據(jù) mysqli_query($mysqli,"INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (1,'Doe','John','456 Park Avenue','New York')"); mysqli_close($mysqli); //關(guān)閉連接
mysqli實(shí)現(xiàn)了面向?qū)ο蟮牟僮鞣绞剑琺ysqli對(duì)象調(diào)用成員方法;而mysql則是基于過(guò)程的過(guò)程式風(fēng)格,每個(gè)mysql函數(shù)調(diào)用時(shí)都需要傳入連接標(biāo)識(shí)符。
mysqli與mysql支持的操作
mysqli(及mysql)的基本操作包括:連接數(shù)據(jù)庫(kù)、查詢(xún)數(shù)據(jù)庫(kù)、批量操作、預(yù)處理和事務(wù)處理等。
//連接數(shù)據(jù)庫(kù) $mysqli = new mysqli("localhost", "root", "", "example"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; } //查詢(xún)數(shù)據(jù)庫(kù) $sql = "SELECT * FROM Persons"; $result = $mysqli->query($sql); //預(yù)處理SQL語(yǔ)句 $stmt = $mysqli->prepare("INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (?, ?, ?, ?, ?)"); //參數(shù)綁定 $stmt->bind_param("issss", $PersonID, $LastName, $FirstName, $Address, $City); //循環(huán)綁定數(shù)據(jù) foreach ($persons as $person) { $PersonID = $person['PersonID']; $LastName = $person['LastName']; $FirstName = $person['FirstName']; $Address = $person['Address']; $City = $person['City']; $stmt->execute(); } //開(kāi)啟事務(wù)處理 $mysqli->begin_transaction(); //執(zhí)行SQL語(yǔ)句 mysqli_query($mysqli,"INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (1,'Doe','John','456 Park Avenue','New York')"); //commit提交 $mysqli->commit(); $mysqli->close();
mysqli與mysql的優(yōu)缺點(diǎn)
mysqli相對(duì)于mysql擴(kuò)展來(lái)說(shuō),優(yōu)缺點(diǎn)分別如下:
- 支持面向?qū)ο蠛突谶^(guò)程,易用性高;
- 支持mysqli->prepare()預(yù)處理SQL語(yǔ)句,可以有效防止SQL注入;
- 支持MySQLi批處理;
- 支持事務(wù)處理,可以保證SQL操作的原子性;
然而,mysqli的一點(diǎn)不足之處就是不支持php4及更早版本,此外,mysqli在一些新特性上被認(rèn)為是MYSQLI擴(kuò)展開(kāi)發(fā)的新標(biāo)準(zhǔn),官方不會(huì)再繼續(xù)維護(hù)和更新mysql擴(kuò)展。
總結(jié)
mysqli是mysql擴(kuò)展的新版,支持事務(wù)處理和面向?qū)ο螅鴐ysql則是php早期的數(shù)據(jù)庫(kù)擴(kuò)展。mysqli受到php社區(qū)的推薦和支持,支持較多新特性,相比mysql擴(kuò)展是更值得推薦使用的數(shù)據(jù)庫(kù)擴(kuò)展之一。當(dāng)然由于歷史原因或其他原因使用mysql也是可以的,但需要注意一些常見(jiàn)的SQL注入、事務(wù)處理等問(wèn)題。