常規(guī)方式
創(chuàng)新互聯(lián)建站主營泌陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應用開發(fā),泌陽h5小程序制作搭建,泌陽網(wǎng)站營銷推廣歡迎泌陽等地區(qū)企業(yè)咨詢
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實都是為了PHP解析XML來做準備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xià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é)點,進而獲取相關(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)。核心在于維護一個“池”。
從池子中取,用畢,歸還給池子。
?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文件丟失,無法進行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準備好數(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 );
}
}
}
數(shù)據(jù)庫有很多種類:mysql,oracle,mssql,db2等等。PHP操作數(shù)據(jù)庫的時候,要保證該類型數(shù)據(jù)庫的擴展已開啟。這里連接的數(shù)據(jù)庫以mysql為例:
?php
//數(shù)據(jù)庫服務器地址
$host="localhost";?
//連接數(shù)據(jù)庫用戶名
$uname="root";?
//連接數(shù)據(jù)庫密碼
$upass="";?
//連接數(shù)據(jù)庫
$conn=mysql_connect($host,?$uname,$upass);
//判斷連接
if(!$conn){
die("連接數(shù)據(jù)庫失敗!").mysql_errno();????
}
//連接成功,其他操作省略
?
在應用配置目錄或者模塊配置目錄(不清楚配置目錄位置的話參考配置章節(jié))下面的database.php中(后面統(tǒng)稱為數(shù)據(jù)庫配置文件)配置下面的數(shù)據(jù)庫參數(shù):
return [
// 數(shù)據(jù)庫類型
'type' = 'mysql',
// 服務器地址
'hostname' = '127.0.0.1',
// 數(shù)據(jù)庫名
'database' = 'thinkphp',
// 數(shù)據(jù)庫用戶名
'username' = 'root',
// 數(shù)據(jù)庫密碼
'password' = '',
// 數(shù)據(jù)庫連接端口
'hostport' = '',
// 數(shù)據(jù)庫連接參數(shù)
'params' = [],
// 數(shù)據(jù)庫編碼默認采用utf8
'charset' = 'utf8',
// 數(shù)據(jù)庫表前綴
'prefix' = 'think_',
];
系統(tǒng)默認支持的數(shù)據(jù)庫type包括:
type 數(shù)據(jù)庫
mysql MySQL
sqlite SqLite
pgsql PgSQL
sqlsrv SqlServer
type參數(shù)支持命名空間完整定義,不帶命名空間定義的話,默認采用\think\db\connector作為命名空間,如果使用應用自己擴展的數(shù)據(jù)庫驅(qū)動,可以配置為:
// 數(shù)據(jù)庫類型
'type' = '\org\db\Mysql',
表示數(shù)據(jù)庫的連接器采用 \org\db\Mysql類作為數(shù)據(jù)庫連接驅(qū)動,而不是默認的\think\db\connector\Mysql。
PHP連接數(shù)據(jù)庫之PHP連接MYSQL數(shù)據(jù)庫代碼
?php???
$mysql_server_name='localhost';?
//改成自己的mysql數(shù)據(jù)庫服務器??
$mysql_username='root';?
//改成自己的mysql數(shù)據(jù)庫用戶名??
$mysql_password='12345678';?
//改成自己的mysql數(shù)據(jù)庫密碼??
$mysql_database='mycounter';
//改成自己的mysql數(shù)據(jù)庫名??
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);???
$sql='CREATE?DATABASE?mycounter?
DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;???
';???
mysql_query($sql);???
$sql='CREATE?TABLE?`counter`?
(`id`?INT(255)?UNSIGNED?NOT?NULL?
AUTO_INCREMENT?,`count`?INT(255)?
UNSIGNED?NOT?NULL?DEFAULT?0,PRIMARY?KEY?
(?`id`?)?)?TYPE?=?innodb;';???
mysql_select_db($mysql_database,$conn);???
$result=mysql_query($sql);???
//echo?$sql;???
mysql_close($conn);???
echo?"Hello!數(shù)據(jù)庫mycounter已經(jīng)成功建立!";???
??
PHP連接數(shù)據(jù)庫之PHP連接ACCESS數(shù)據(jù)庫代碼方法
???
$conn?=?new?com("ADODB.Connection");???
$connstr?=?"DRIVER={Microsoft
Access?Driver?(*.mdb)};?
DBQ=".?realpath("data/db.mdb");???
$conn-Open($connstr);???
$rs?=?new?com("ADODB.RecordSet");???
$rs-Open("select?*
from?szd_t",$conn,1,1);???
while(!?$rs-eof)?{???
$f?=?$rs-Fields(1);???
echo?$f-value;???
$rs-MoveNext();???
}???
?
PHP連接mysql數(shù)據(jù)庫是PHP新手們必須要掌握的一項技能,只要掌握了PHP對數(shù)據(jù)庫進行增刪改查等操作,就可以寫出一些簡單且常見的程序。如留言表,新聞頁等。本篇文章主要給大家詳細介紹PHP連接Mysql數(shù)據(jù)庫的兩種常用方法。
下面我們通過具體的代碼示例來給大家詳細介紹兩種PHP連接mysql數(shù)據(jù)庫的方法。
mysqli連接數(shù)據(jù)庫和pdo連接數(shù)據(jù)庫。
第一種方法:使用mysqli連接mysql數(shù)據(jù)庫
代碼實例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$link=new mysqli($host,$user,$password,$dbName);
if ($link-connect_error){
die("連接失敗:".$link-connect_error);
}
$sql="select * from admins";
$res=$link-query($sql);
$data=$res-fetch_all();
var_dump($data);
在經(jīng)過一系列的連接操作后,我們再創(chuàng)建一個sql語句對其中數(shù)據(jù)表進行查詢檢驗。在上述代碼中,我們要先創(chuàng)建一些需要用到的變量,如數(shù)據(jù)庫用戶名、數(shù)據(jù)庫名密碼等。然后我們用面向?qū)ο蟮姆绞竭B接了名為php的數(shù)據(jù)庫。再通過if條件語句,connect-error方法判斷PHP連接數(shù)據(jù)庫是否成功。
這里我們先登錄phpmyadmin看看是否存在php數(shù)據(jù)庫,從下圖可以知道是存在php這個數(shù)據(jù)庫的。
最后通過瀏覽器訪問,結(jié)果如下圖:
從圖中可以得知,我們成功地連接了php數(shù)據(jù)庫,并且能查詢出數(shù)據(jù)表信息。
第二種方法:使用PDO連接數(shù)據(jù)庫
代碼示例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$pdo=new PDO("mysql:host=$host;dbname=$dbName",$user,$password);
$sql="select * from admins";
$data=$pdo-query($sql)-fetch();
var_dump($data);
PHP連接Mysql步驟以上就是關(guān)于PHP連接數(shù)據(jù)庫查詢數(shù)據(jù)的兩種常用方法詳解,更多相關(guān)教程請訪問php中文網(wǎng)mysql視頻教程,歡迎參考學習
分享標題:PHP連接神通數(shù)據(jù)庫,PHP連接數(shù)據(jù)庫代碼
URL網(wǎng)址:http://chinadenli.net/article8/heciop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、微信公眾號、外貿(mào)建站、搜索引擎優(yōu)化、網(wǎng)站建設(shè)、用戶體驗
聲明:本網(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)