Home  >  Article  >  Backend Development  >  Access multiple databases using PHPLIB_PHP tutorial

Access multiple databases using PHPLIB_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:31747browse

PHPLIB是PHP的一些扩展库,使用它我们可以很方便地对数据库进行各种操作,不过,如果你要使用多个数据库的话,它就显得力不从心了,本文介绍了通过扩展PHPLIB,让你鱼和熊掌兼得,在使用PHPLIB的同时可以使用多个数据库,而且从中你也可以了解到面向对象编程和如何扩展库的知识,值得一读。

数据库管理

   你可以在一个大型的数据库中放入任何表。不过时间长了,将会令数据库变得越来越大,服务器可能会跟不上IO的工作,或者没有足够的内存应付所有的访问?要分开现有的数据又非常难。明智的办法是开始时就使用分开的数据库,并且进行有效的数据库管理。 如果你有一个卖书的网站,你可能有作者的列表,书价的列表,还有当前的库存和订单的列表。当你的业务不断增长时,订单将会不断地增长,而且处理每个订单都需要进行很多的磁盘访问。很可能你将在某一天将所有的订单都放到一个会计系统中。

  现在就将订单放到一个独立的数据库吧。由于库存也是通过订单更新的,因此库存量也放到同样的数据库中。

  作者的列表和书的列表都是一些静态的信息,要经常读取,但很少更新。实际上,更新一个作者的记录可能只需要每5年一次,只在作者写了一本新书(或者去世)时进行。放这些数据的服务器的配置可与放订单数据库的服务器完全不同。

包含PHPLIB

  PHPLIB通过一个称为DB_Sql的类访问SQL数据库。根据你需要使用的数据库类型,将不同的inc文件包含在你的代码中。在这个例子中,我使用MySQL的版本。

  为了在你的代码中使用DB_Sql,要将PHPLIB文件安装在它们自己的目录中。然后,找到你的cgi-bin目录,并且在cgi-bin的目录旁创建phplib目录。下一步,拷贝所有的PHPLIB .inc文件到phplib目录。最后,修改php.inc文件,只要将“include_path=”的行改为该phplib目录就可以了。

include_path是PHP使用include()或者require()时查找的目录,在我的NT workstation中,include的路径是:

include_path = ".;i:/project52/includes;i:/project52/phplib";

在Linux的系统上

include_path = ".;/home/httpd/includes;/home/httpd/phplib";

在每个PHP页面的顶部加入
<?php
require(common.php);
?>
common.php3放在includes目录中,包含了每个页面要用到的所有数据和函数。在这个例子中的common.php是:
<?php
require(db_mysql.inc);
require(ct_sql.inc);
require(session.inc);
require(auth.inc);
require(perm.inc);
require(user.inc);
require(page.inc);
?>

  如果你想知道每个inc文件的用处,可阅读http://phplib.netuse.de上的PHPLIB文档。Db_mysql.inc包含了所有DB_SQL类的定义。如果你想使用PostGreSQL代替MySQL,只要用db_pgsql.inc代替db_mysql.inc就可以了。还有10个其它的.inc文件,可以使用MS SQL、Oracle、Sybase或者其它的数据库。

  要注意的是,在这个例子中,require()和include()是完全一样的。不过,如果放在代码中,或者在if语句中使用时,Require()和include的使用是完全不同的,并且有不同的运行结果。

扩展PHPLIB

  PHPLIB通过一个DB_Sql类产生的对象来访问数据库。Db_mysql.inc包含了为MySQL修改过的DB_Sql类。我们将通过在common.php中加入代码来扩展DB_sql,这些代码将加在包含db_mysql.inc的行后。

DB_Sql包含了很多用作查询的函数,我们要作修改的是:

<?php
/* public: 连接管理 */
function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* 处理默认连接 */
if ("" == $Database)
$Database = $this->Database;
if ("" == $Host)
$Host = $this->Host;
if ("" == $User)
$User = $this->User;
if ("" == $Password)
$Password = $this->Password;

/* 建立连接,选择数据库 */
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mysql_pconnect($Host, $User, $Password);
if (!$this->Link_ID) {
$this->halt("pconnect($Host, $User, $Password) failed.");
return 0;
}
if (Link_ID">!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
return 0;
}
}

return $this->Link_ID;
}
?>

  在你的db_mysql.inc(或者其它数据库的相关.inc文件)中找到connect()函数,然后将它拷贝到common.php,放到包含db_mysql.inc代码的后面,在后面,还要将它封装为一个类的定义。

I found the code a bit hard to read, so I first made the copied code more readable:

/* public: connection management*/
function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Processing default Connection*/
if ("" == $Database) {
$Database = $this->Database;
}
if ("" == $Host) {
$ Host = $this->Host;
}
if ("" == $User) {
$User = $this->User;
}
if ("" == $Password) {
$Password = $this->Password;
}
/* Establish connection, select database*/
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mysql_pconnect($Host, $User, $Password);
if (!$this->Link_ID) {
$this->halt("pconnect( $Host, $User, $Password) failed.");
return 0;
}
if (Link_ID">!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
return 0;
}
}
return $this->Link_ID;
}
?>

I adjusted the position of the brackets and added a brace before and after the single line. In PHP's if statement, you don't need parentheses if there is only one line of code, but if you add one more line of code, an error will occur immediately. Therefore I suggest you add a bracket to avoid errors when adding code later.

Before changing the connect code, you must first understand how connect() works. It checks whether a connection currently exists. If there is no connection, it creates a connection. Before each database query, first run this connect() function. Unfortunately, it only selects the database when connecting for the first time. If your PHP page uses more than one database, connect() will not select another database.

There are a few different ways to change the code. We need to choose a method that has the least impact on PHPLIB and allows us to display the database connection status when we need to analyze the problem. We need to save the connection id and database name outside of PHPLIB. Just add in common.php:

$db_connection = 0; // Database connection id
$db_database = ""; // Current database status
? >

Next, we need to modify PHPLIB to store the connection id and database name in these variables. You can set and use the same variable name in other code. When analyzing the problem, if you need to know which database is being used, just insert the following code into the page:

Print(" db_database: " . $db_database . "");
? >

How can we make connect() use these new variables? We can add a line at the top:

{
globals $db_connect, $db_database;
/* Handle defaults */
? >

Through these codes, the new variables can be accessed by connect()

After defining $db_database, add:
function db_connect($db_connect_host="", $db_connect_user="",$db_connect_pass="") {
globals $db_connect;
if(!empty($db_connect_host)) {
$ db_connect = mysql_pconnect($db_connect_host,
$db_connect_user, $db_connect_pass);
}
return($db_connect);
}
function db_database($db_database_new="") {
globals $db_database;
if(!empty($db_database_new)) {
$db_database = @mysql_select_db($db_database_new, db_connect());
}
return($db_database);
}
? >

As long as you define these public functions once, you can use these public variables in different places without adding global declarations. The following are the public functions that use the above db function:

function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Handle default connection*/
if ("" = = $Database) {
$Database = $this->Database;
}
if ("" == $Host) {
$Host = $this->Host;
}
if ("" == $User) {
$User = $this->User;
}
if ("" == $Password) {
$Password = $this->Password;
}
/* Establish connection, select database*/
if ( 0 == db_connect()) {
$this->Link_ID = db_connect($ Host, $User, $Password);
if (!$this->Link_ID) {
$this->halt("pconnect($Host, $User, $Password) failed.");
return 0;
}
}
if (0 != db_connect()) {
if($Database != db_database()) {
$this->Database = db_database($Database))
if(empty($this->Database)) {
$this->halt("cannot use database " . $this->Database);
return 0;
}
}
}
return $this->Link_ID;
}
? >

Look out for the following changes:

The test of the database is separated from the test of the connection, so that even if connect() has a current connection, it can still check whether to change to another database. This means that db_connect() compares to 0 twice as often as before,

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486635.htmlTechArticlePHPLIB is some extension library of PHP. Using it, we can easily perform various operations on the database. However, If you want to use multiple databases, it will be overpowered. This...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn