Using Phplib to access multiple databases

Source: Internet
Author: User
Tags contains extend functions include mysql require return
Access | data | Database phplib is a PHP extension library, using it we can easily to the database for a variety of operations, but if you want to use more than one database, it seems powerless, this article introduces the expansion of the phplib, so that you have the fish and bear cake, You can use multiple databases while using Phplib, and you can also learn about object-oriented programming and how to extend the library's knowledge, which is worth reading.

Database management

You can put any table in a large database. But for a long time, the database will become larger, the server may not be able to keep up with IO work, or not enough memory to meet all the access? It's very difficult to separate existing data. It is advisable to start with a separate database and to manage the database effectively. If you have a book-selling website, you may have a list of authors, a list of book prices, and a list of current inventory and orders. As your business grows, orders will grow, and many disk accesses are required to process each order. It is likely that one day you will put all your orders in an accounting system.

Now put the order in a separate database. Because inventory is also updated by order, inventory is also placed in the same database.

The list of authors and lists of books are static information that is often read, but rarely updated. In fact, updating an author's record may only take place once every 5 years, only when the author writes a new book (or dies). The configuration of the server where the data is placed can be completely different from the server placing the order database.

Contains Phplib

Phplib accesses the SQL database through a class called Db_sql. Depending on the type of database you need to use, include different inc files in your code. In this example, I use the version of MySQL.

To use Db_sql in your code, install the Phplib files in their own directory. Then, locate your Cgi-bin directory and create the Phplib directory next to the Cgi-bin directory. Next, copy all Phplib. inc files to the Phplib directory. Finally, to modify the Php.inc file, simply change the "include_path=" line to the Phplib directory.

Include_path is the directory that PHP looks for when using include () or require (), and in my NT Workstation, the path to include is:

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

On a Linux system

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

Join at the top of each PHP page
? Php

Require (common.php);

? >
COMMON.PHP3 is placed in the includes directory and contains all the data and functions you want to use for each page. The common.php in this example are:
? Php

Require (DB_MYSQL.INC);
Require (CT_SQL.INC);
Require (SESSION.INC);
Require (AUTH.INC);
Require (PERM.INC);
Require (USER.INC);
Require (PAGE.INC);

? >

If you want to know the usefulness of each inc file, read the Phplib documentation on Http://phplib.netuse.de. Db_mysql.inc contains the definitions for all Db_sql classes. If you want to use PostgreSQL instead of MySQL, just use db_pgsql.inc instead of db_mysql.inc. There are 10 other. inc files that can use Ms SQL, Oracle, Sybase, or other databases.

Note that in this example, the require () and include () are exactly the same. However, if placed in code, or used in an if statement, the use of Require () and include is completely different and has different running results.

Extended Phplib

Phplib access to the database through an object generated by a Db_sql class. Db_mysql.inc contains db_sql classes that have been modified for MySQL. We will extend the db_sql by adding code to the common.php, which will be added after the line containing Db_mysql.inc.

Db_sql contains a number of functions used as queries, and we want to make changes to:

? Php

/* Public: Connection Management */
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 a connection, select the 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 (! @mysql_select_db ($Database, $this->link_id)) {
$this->halt ("Cannot use database" $this->database);
return 0;
}
}

return $this->link_id;
}

? >

[1] [2] [3] [4] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.