當前位置:首頁 » 數據倉庫 » php網站怎麼連接資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

php網站怎麼連接資料庫

發布時間: 2022-12-17 04:40:27

1. dw中php怎麼連接mysql資料庫

首先打開DW,找到資料庫 選項卡:

這里是說,要完成連接資料庫操作 需要完成3步。那麼好,我們按步驟來做:

1、搞定創建站點

點擊藍色「站點」,彈出如下界面

站點命名

伺服器技術

編輯和測試文件存放位置,我們都在本地開發的,所以都在本地文件存在的位置就選你網站代碼的位置

定義瀏覽器打開預覽時url路徑,我這里是:http://localhost/news,注意這個路徑一定是可以訪問的路徑,就是在瀏覽器中能打開,否則4步的mysql會報連接不成功的操作

是否共享文件,這個是用來團隊開發的,點否

OK,下一步,完成,收功。這樣我們就完成了第一步,多出來一個√,增加了一個第四步。

2、搞定文檔類型

點擊藍色「文檔類型」,彈出如下界面

我們用的PHP,這里選擇PHP

完成,ok,第二步搞定。看又多出一個√。

3、搞定測試伺服器

點擊藍色「測試伺服器」,彈出如下界面

顯示我們第一步已經配置過的內容,直接點確認

完成,ok,但是並沒有按照預想多出一個√,沒事,繼續。

4、創建MYSQL連接

點擊上圖紅色箭頭指的+,出來Mysql連接,點擊,彈出如下界面

依次輸入你本地的信息,如我這里輸入的

然後點選取,彈出你的資料庫,選擇你的項目news所用的資料庫。確定,確定,ok,完成。

2. PHP網站怎麼連接到資料庫

常規方式

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

// 讀取配置文件內容
$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;
}
}

資料庫連接池

對於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 );
}
}
}