PHP鏈接數(shù)據(jù)庫(kù)有幾種方式
荊門ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
mysqli:
?php?
$servername?=?"localhost";?
$username?=?"username";?
$password?=?"password";?
//?創(chuàng)建連接?
$conn?=?new?mysqli($servername,?$username,?$password);?
//?檢測(cè)連接?
if?($conn-connect_error)?{
die("連接失敗:?"?.?$conn-connect_error);?
}?
echo?"連接成功";?
?
也可以使用PDO進(jìn)行鏈接,前提是你必須在php.ini中開啟PDO:
?php
$servername?=?"localhost";
$username?=?"username";
$password?=?"password";
try?{
$conn?=?new?PDO("mysql:host=$servername;dbname=myDB",?$username,?$password);
echo?"連接成功";?
}
catch(PDOException?$e)
{
echo?$e-getMessage();
}
?
建議使用PDO,功能更加強(qiáng)大,兼容各種數(shù)據(jù)庫(kù)
1、用 mysql_connect 的方法,PHP7會(huì)報(bào)致命錯(cuò)誤
$conn= mysql_connect('localhost','xueyanxiang','xueyanxiang');
Fatal error : Uncaught Error: Call to undefined function mysql_connect() in /Users/xueyanxiang/work/test/xue.php:31 Stack trace: #0 /Users/xueyanxiang/work/test/xue.php(119): xue-run() #1 {main} thrown in? /Users/xueyanxiang/work/test/xue.php ?on line? 31
原因是:
PHP5中使用mysql_connect()函數(shù)進(jìn)行連接,但實(shí)際上,PHP5.5開始,MySQL就不推薦使用了,屬于廢棄函數(shù)
PHP7中貌似已經(jīng)徹底不支持了,根據(jù)官網(wǎng)說(shuō)明,取而代之的是如下兩個(gè):
本擴(kuò)展自 PHP 5.5.0 起已廢棄,并在將來(lái)會(huì)被移除。應(yīng)使用?MySQLi?或?PDO_MySQL?擴(kuò)展來(lái)替換之。參見?MySQL:選擇
API?指南以及相關(guān) FAQ?以獲取更多信息。用以替代本函數(shù)的有:
mysqli_connect()
PDO::__construct()
使用時(shí),不要在使用mysql_connect了,可以換用mysqli_connect(),用法基本類似吧,據(jù)說(shuō)是面向?qū)ο蟮膸?kù)。
php.ini中,也只有extension=php_mysqli.dll,而不再有extension=php_mysql.dll這個(gè)拓展了。
2、可以使用mysqli,對(duì)象化,方法名與被廢棄的類似
$conn= mysqli_connect('localhost','xueyanxiang','xueyanxiang');
3、PDO工具,推薦使用
$dbh= "mysql:host=localhost;dbname=test";
$db= new PDO($dbh,'xueyanxiang','xueyanxiang');
$objQuery= $db-query("select * from user;");
$res= $objQuery-fetchAll(PDO::FETCH_ASSOC);
不填寫參數(shù)的話,默認(rèn)是關(guān)聯(lián)和索引都有,如下圖
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫(kù);
檢查php環(huán)境是否已開啟mysql擴(kuò)展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項(xiàng);b.打開php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫(kù)主機(jī)地址
$host="localhost";
//定義mysql數(shù)據(jù)庫(kù)登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫(kù)登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫(kù)
$conn = mysql_connect($host,$user,$pwd);
//對(duì)連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫(kù)連接失敗!".mysql_errno());
}else{
echo "數(shù)據(jù)庫(kù)連接成功!";
}
?
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實(shí)都是為了PHP解析XML來(lái)做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對(duì)于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請(qǐng)按照此標(biāo)準(zhǔn)順序書寫.其實(shí)也無(wú)所謂了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作為解析XML配置文件必備工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {
$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內(nèi)容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點(diǎn),進(jìn)而獲取相關(guān)的數(shù)據(jù)庫(kù)信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(guān)聯(lián)數(shù)組,方便接下來(lái)的方法調(diào)用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫(kù)配置文件信息出錯(cuò)!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫(kù)連接池
對(duì)于PHP程序而言,優(yōu)化永無(wú)止境。而數(shù)據(jù)庫(kù)連接池就在一定程度上起到了優(yōu)化的作用。其使得對(duì)用戶的每一個(gè)請(qǐng)求而言,無(wú)需每次都像數(shù)據(jù)庫(kù)申請(qǐng)鏈接資源。而是通過(guò)已存在的數(shù)據(jù)庫(kù)連接池中的鏈接來(lái)返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。
于是,這里簡(jiǎn)單的模擬了一下數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫(kù) 工具類設(shè)計(jì)
* ?郭璞
* ?2016年12月23日
*
**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無(wú)法進(jìn)行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫(kù)連接池“偽隊(duì)列”
$this-poolsize = $poolsize;
$this-dbpool = array (); ? ? ? ?for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark連接數(shù)據(jù)庫(kù)失??!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫(kù)連接池中獲取一個(gè)數(shù)據(jù)庫(kù)鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池中已無(wú)鏈接資源,請(qǐng)稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫(kù)鏈接資源放回到數(shù)據(jù)庫(kù)連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫(kù);
檢查php環(huán)境是否已開啟mysql擴(kuò)展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項(xiàng);b.打開php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫(kù)主機(jī)地址
$host="localhost";
//定義mysql數(shù)據(jù)庫(kù)登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫(kù)登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫(kù)
$conn = mysql_connect($host,$user,$pwd);
//對(duì)連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫(kù)連接失敗!".mysql_errno());
}else{
echo "數(shù)據(jù)庫(kù)連接成功!";
}
?
方法/步驟
1
登錄到phpMyAdmin
2
新建一個(gè)要導(dǎo)入的數(shù)據(jù)庫(kù),點(diǎn)擊“+new”
3
如圖,分別填寫數(shù)據(jù)庫(kù)名稱,以及選擇數(shù)據(jù)庫(kù)的排序規(guī)則,
4
完成步驟3,點(diǎn)擊“創(chuàng)建”
5
完成步驟4,從右側(cè)就可以看到我們新創(chuàng)建的數(shù)據(jù)庫(kù)了,如果沒有立即顯示,刷新即可立馬顯示了。
6
單擊我們新創(chuàng)建的數(shù)據(jù)庫(kù),
7
然后,我們選擇“導(dǎo)入”,
8
完成步驟7,我們點(diǎn)擊“選擇文件”,
9
點(diǎn)擊了“選擇文件”之后,就會(huì)出現(xiàn)如圖所示的彈出框,我們選擇要導(dǎo)入的sql就可以了,后綴名可以是.sql,也可以是壓縮文件.zip。如圖,選擇好文件之后,點(diǎn)擊“確定”就可以了
10
完成步驟⑨,你就可以從剛才的頁(yè)面中看到自己上傳的文件了,如圖所示,其余選項(xiàng)默認(rèn)就可以了,然后點(diǎn)擊“執(zhí)行”就可以了。
11
這個(gè)時(shí)候,你就可以看到你的數(shù)據(jù)已經(jīng)導(dǎo)入到你新創(chuàng)建的數(shù)據(jù)庫(kù)中了
當(dāng)前標(biāo)題:php如何鏈接數(shù)據(jù)庫(kù) php建立數(shù)據(jù)庫(kù)連接的代碼
URL網(wǎng)址:http://muchs.cn/article16/docogdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、Google、微信公眾號(hào)、網(wǎng)站導(dǎo)航、定制開發(fā)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容