PHP网站怎么连接到数据库?

PHP网站怎么连接到数据库?,第1张

常规方式

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

// 读取配置文件内容

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

PHP解析XML

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

配置文件

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

<!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 -->

<host>localhost</host>

<user>root</user>

<password>123456</password>

<db>test</db>

<port>3306</port></mysql>12345678910

解析

<?php/**

* 作为解析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 ( "<mark>读取数据库配置文件信息出错!</mark><br />" )

}        return $dbconfig

}

}1234567891011121314151617181920212223242526272829

数据库连接池

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

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

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

<?php/**x

*  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 ( "<mark>utils.php文件丢失,无法进行配置文件的初始化 *** 作!</mark><br />" )

}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 ( "<mark>连接数据库失败!</mark><br />" )

array_push ( $this->dbpool, $conn )

}

}    /**

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

*

* @throws ErrorException

* @return mixed

*/

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

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

}

}    /**

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

*

* @param unknown $conn

* @throws ErrorException

*/

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

} else {

array_push ( $this->dbpool, $conn )

}

}

}

php面向对象文件名DB.class.php

<?php

     header("content-type:text/htmlcharset=utf-8")

     class DB{

     public $db_host//localhost

 public $db_user//用户名

 public $db_pwd//密码

 public $db_name//数据库名

 public $links//链接名称

 //构造方法的参数和属性名字一致,但是含义不同

 function __construct($db_host,$db_user,$db_pwd,$db_name){

     $this -> db_host = db_host

 $this -> db_user = db_user

 $this -> db_pwd = db_pwd

 $this -> db_name = db_name

 //链接数据库代码

 $this -> links = @mysql_connect($db_host,$db_user,$db_pwd)or die("数据库链接失败")

 //echo 派段穗$this -> links打印是资源

 mysql_query("set names utf8")

 mysql_select_db($db_name,$this->links)

 

 }

 function query($sql){//执行各种sql,inert update delete执行,如果执行select返回结果集

     return mysql_query($sql)

 }

 function numRows($sql){//返回select的记录数

     $result = $this -> query($sql)

 $count = mysql_num_rows($result)

 return $count

 }

 function getOne($sql){//得到一条记录的一维数组

     $result = $this -> query($sql)

 $arr = mysql_fetch_assoc($result)

 return $arr

 }

 function getAll($sql){//得到多条记录的二维数组

     $result = $this -> query($sql)

 $rows = array()

 while($rs = mysql_fetch_assoc($result)){

     $rows[] = 燃乎$rs

 }

 return $rows

 }

 function __destruct(){

     $this -> db_host = db_host

 $this -> db_user = db_user

 $this -> db_pwd = db_pwd

 $this -> db_name = db_name

 }

 }

 

 $db = new DB("localhost","root","","car")

 //$sql = "insert into category(categoryName)values('常熟seo')"

 //$db -> query($sql)

 

 //返回select的记录数

 //$sql = "select * from category"

 //$count = $db -> numRows($sql)

 //echo $count

 

 //得到一条记录的一维数组

 //$sql = "select * from category where categoryId=1"

 //$arr = $db -> getOne($sql)

 //print_r($arr)

 

 //得到多条记录的二维数组

 $sql = "select * from category"

 $rs = $db -> getAll($sql)

 print_r($rs)

 

 

?>尘卜

创建一个数据库大类

重新看了一下··大概明白楼主意思了。

你是不是下载了别人一个什么破J或是开源的系统?

现在不知道怎么连接数据?

在使用之前,您需要分别配置一纯基下文腔数件:

1.Connections/conn.php

2.api/conn.php

3.admin/connect.php

直接打开自己的网站,找到这三个PHP文件,然后将里面默认的伍裤首数据库数据库名、数据库账号、数据库密码改为自己的数据库名、账号、密码就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12125240.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存