php連接數(shù)據(jù)庫命令 php 數(shù)據(jù)庫連接

PHP網(wǎng)站怎么連接到數(shù)據(jù)庫?

常規(guī)方式

創(chuàng)新互聯(lián)公司是一家專業(yè)提供思禮企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為思禮眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。

// 讀取配置文件內(nèi)容

$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123

PHP解析XML

上述兩種讀取文件,其實都是為了PHP解析XML來做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。

配置文件

?xml version="1.0" encoding="UTF-8" ?mysql

!-- 為防止出現(xiàn)意外,請按照此標(biāo)準(zhǔn)順序書寫.其實也無所謂了 --

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é)點,進(jìn)而獲取相關(guān)的數(shù)據(jù)庫信息

$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點信息賦值給關(guān)聯(lián)數(shù)組,方便接下來的方法調(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ù)庫配置文件信息出錯!/markbr /" );

} ? ? ? ?return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

數(shù)據(jù)庫連接池

對于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對用戶的每一個請求而言,無需每次都像數(shù)據(jù)庫申請鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時間上,效率上,都是一個大大的提升。

于是,這里簡單的模擬了一下數(shù)據(jù)庫連接池的實現(xiàn)。核心在于維護(hù)一個“池”。

從池子中取,用畢,歸還給池子。

?php/**x

* ?PHP中的數(shù)據(jù)庫 工具類設(shè)計

* ?郭璞

* ?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文件丟失,無法進(jìn)行配置文件的初始化操作!/markbr /" );

}else {

require './utils.php';

} ? ? ? ?// 初始化 配置文件信息

$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫連接池“偽隊列”

$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ù)庫失敗!/markbr /" );

array_push ( $this-dbpool, $conn );

}

} ? ?/**

* 從數(shù)據(jù)庫連接池中獲取一個數(shù)據(jù)庫鏈接資源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池中已無鏈接資源,請稍后重試!/mark" );

} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );

}

} ? ?/**

* 將用完的數(shù)據(jù)庫鏈接資源放回到數(shù)據(jù)庫連接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池已滿/markbr /" );

} else {

array_push ( $this-dbpool, $conn );

}

}

}

php怎么連接數(shù)據(jù)庫

直接寫代碼啊。

我寫了一遍截圖看。第一行參數(shù)主機、用戶名、密碼;第二行選擇數(shù)據(jù)庫‘第三行選擇字符集’

你自己試下

如何在PHP里面連接數(shù)據(jù)庫

12mysql_connect();// 這個函數(shù)是連接數(shù)據(jù)庫所在服務(wù)器的mysql_select_db();// 這個函數(shù)是連接具體數(shù)據(jù)庫的如果是要聯(lián)接同一個服務(wù)器里的不同數(shù)據(jù)庫里的表,那么就不需要第二個函數(shù),直接在sql語句里進(jìn)行選擇就可以了。select * from `庫`.`表` where 1就是在表達(dá)表的時候前面用庫進(jìn)行修飾

怎樣用PHP連接sql數(shù)據(jù)庫

1、學(xué)習(xí)的方法,最好是看手冊

在前一篇文章中涉及到了連接數(shù)據(jù)庫sqlsrv_connect();還記得我們mysql連接數(shù)據(jù)庫的時候也是mysql_connect();兩者操作數(shù)據(jù)庫是很相似的。從零基礎(chǔ)開始學(xué)起的話,只能是查閱手冊。查看一下sqlsrv有哪些函數(shù)提供我們使用!推薦網(wǎng)址是:

【注意】很多的方法都跟mysql提供的方法很相似,看到后面的函數(shù)名就大概知道里面的用法,比如說mysql執(zhí)行sql語句的時候調(diào)用mysql_query();而sqlserver執(zhí)行sql語句的時候調(diào)用也是sqlsrv_query(),但是特別注意一點就是,它們的傳遞參數(shù)不一樣。詳細(xì)的只能看一下手冊。接下來我簡單總結(jié)一下操作數(shù)據(jù)的方法

2、連接數(shù)據(jù)庫sqlsrv_connect()

?php$serverName = "serverName\sqlexpress";//服務(wù)器的名字,本地localhost$connectionInfo = array( "Database"="dbName", "UID"="userName", "PWD"="password");$conn = sqlsrv_connect( $serverName, $connectionInfo);if( $conn ) { ? ? echo "Connection established.br /";

}else{ ? ? echo "Connection could not be established.br /"; ? ? die( print_r( sqlsrv_errors(), true));

}

3、操作數(shù)據(jù)庫

1)執(zhí)行sql語句sqlsrv_query(),返回值為true或者false,這里函數(shù)的用法跟mysql_query(),不一樣。它需要把連接的資源句柄當(dāng)作參數(shù)傳進(jìn)去,看源碼。資源句柄就是上面代碼連接數(shù)據(jù)庫的“$conn”。

$sql = "select * from test1"; ? //sql語句$data = sqlsrv_query($conn,$sql); ?//$conn資源句柄if($data == true){ ? ?die("執(zhí)行成功");

}else{ ? ?die("執(zhí)行失敗");

}

2)獲取結(jié)果集

//以數(shù)值索引數(shù)組、關(guān)聯(lián)數(shù)組或這兩種數(shù)組的形式檢索下一行的數(shù)據(jù)。類似于mysql_fetch_arraysqlsrv_fetch_array ?

//以對象形式檢索下一行的數(shù)據(jù)。sqlsrv_fetch_object 1234

$sql = "select * from test1";$data = sqlsrv_query($conn,$sql);if($data == true){ ? ?while($row = sqlsrv_fetch_array( $data, SQLSRV_FETCH_ASSOC) ) { ? ? ? ? ?echo $row['id'].", ".$row['name']."br /";

}else{ ? ? ? ? die( print_r( sqlsrv_errors(), true));

}

}

$sql = "SELECT fName, lName FROM Table_1";$stmt = sqlsrv_query( $conn, $sql);if( $stmt === false ) { ? ? die( print_r( sqlsrv_errors(), true));

}while( $obj = sqlsrv_fetch_object( $stmt)) { ? ? ?echo $obj-fName.", ".$obj-lName."br /";

}

3)顯示錯誤信息sqlsrv_errors():上面都有用到這個函數(shù),只要是操作數(shù)據(jù)庫發(fā)生錯誤,都可以使用這個函數(shù)打印出來看一下壓

這里就不用上代碼了

php里面怎么鏈接數(shù)據(jù)庫?

php鏈接mysql必備條件:

已安裝mysql數(shù)據(jù)庫;

檢查php環(huán)境是否已開啟mysql擴展(一般情況下是開啟的);

檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。

php鏈接代碼如下:

?php

//設(shè)置編碼格式

header("Content-type:text/html;charset=utf-8");

//定義數(shù)據(jù)庫主機地址

$host="localhost";

//定義mysql數(shù)據(jù)庫登錄用戶名

$user="root";

//定義mysql數(shù)據(jù)庫登錄密碼

$pwd="";

//鏈接數(shù)據(jù)庫

$conn = mysql_connect($host,$user,$pwd);

//對連接進(jìn)行判斷

if(!$conn){

die("數(shù)據(jù)庫連接失??!".mysql_errno());

}else{

echo "數(shù)據(jù)庫連接成功!";

}

?

網(wǎng)頁名稱:php連接數(shù)據(jù)庫命令 php 數(shù)據(jù)庫連接
標(biāo)題路徑:http://www.muchs.cn/article44/doodche.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、動態(tài)網(wǎng)站搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)