popsql连接mysql数据库_PHP网站怎么连接到数据库?

展开全部

常规方式

常规方式就是按部就班的读取636f707962616964757a686964616f31333365646261文件了。其余的话和上述方案一致。

// 读取配置文件内容

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

PHP解析XML

上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。

配置文件

localhost

root

123456

test

330612345678910

解析

* 作为解析XML配置文件必备工具

*/class XMLUtil {

public static $dbconfigpath = "./db.config.xml";    public static function getDBConfiguration() {

$dbconfig = array ();        try {            // 读取配置文件内容

$handle = fopen(self::$dbconfigpath, "r");            $content = fread($handle, filesize(self::$dbconfigpath));            // 获取xml文档根节点,进而获取相关的数据库信息

$mysql = simplexml_load_string($content);            // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用

$dbconfig['host'] = $mysql->host;            $dbconfig['user'] = $mysql->user;            $dbconfig['password'] = $mysql->password;            $dbconfig['db'] = $mysql->db;            $dbconfig['port'] = $mysql->port;            // 将配置信息以关联数组的形式返回

return $dbconfig;

} catch ( Exception $e ) {            throw new RuntimeException ( "读取数据库配置文件信息出错!
" );

}        return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

数据库连接池

对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。

于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。

从池子中取,用毕,归还给池子。

*  PHP中的数据库 工具类设计

*  郭璞

*  2016年12月23日

*

**/class DbHelper {    private $dbconfig;    private $dbpool;    public $poolsize;    public function __construct($poolsize = 20) {        if (! file_exists ( "./utils.php" )) {            throw new RuntimeException ( "utils.php文件丢失,无法进行配置文件的初始化操作!
" );

}else {

require './utils.php';

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

$this->dbconfig = XMLUtil::getDBConfiguration ();        // 准备好数据库连接池“伪队列”

$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 ( "连接数据库失败!
" );

array_push ( $this->dbpool, $conn );

}

}    /**

* 从数据库连接池中获取一个数据库链接资源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() {        if (count ( $this->dbpool ) <= 0) {            throw new ErrorException ( "数据库连接池中已无链接资源,请稍后重试!" );

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

}

}    /**

* 将用完的数据库链接资源放回到数据库连接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) {        if (count ( $this->dbpool ) >= $this->poolsize) {            throw new ErrorException ( "数据库连接池已满
" );

} else {

array_push ( $this->dbpool, $conn );

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值