PHP script database functions (below)

Source: Internet
Author: User
Tags php database
PHP script database function details (below) use classes to accelerate PHP database development
There are many database access functions. Improper use may reduce efficiency and even lead to errors. PHP itself is open and extensible, and many people develop the source code of various features for it. Every PHP programmer should be good at inheriting others' achievements and saving time and energy. Standing on the shoulders of giants can see farther. Of course, you can also share your code to realize the sense of accomplishment brought by your work being recognized and created.

The use of database classes allows us to focus on program development without having to consider specific database types.

Among the many development kits, PHPLib has stable performance and complete functions. PHPLib can be obtained in http://phplib.netuse.de. It contains database support classes. Take MySQL database as an example. PHPLib comes with a class named DB_ SQL. It encapsulates functions such as database connection, query, result fetch, and database table traversal.

The use of database classes allows us to focus on program development without having to consider specific database types. The program code does not need to be changed even if the database system type is changed. At the same time, the database class provides a complete and robust database access method, which may be the biggest advantage of using the class packaging.
Next, we will use the database class provided by PHPLib to access the database we just created and display the content.

<〈?

Require "db_mysql.php ";

// Generate a file containing the database class

$ Db = new DB_ SQL;

// Declare a database instance

$ Db-> connect ("ResumeDB", "localhost", "root ","");

// Connect to the database server

// The following parameters are provided: database name, host name, user name, and user password.

If ($ db-> Link_ID)

// Determine whether the connection is established correctly

{

$ Db-> query ("select ID, Name, Intro FROM Resume ");

// Query

If ($ db-> nf ())

// Determine whether the result set is null

{

While ($ db-> next_record ())

// Obtain the value of the next record until the record set content is obtained

{

Echo "ID:", $ db-> f ("ID"); // The f () function returns the value of a subsegment of the current record.

Echo "<br> 〉";

Echo "name :";

$ Db-> p ("Name ");

// The p () function directly prints the value of a sub-segment

// Equivalent to echo $ db-> f ("name ")

Echo "<br> 〉";

Echo "Introduction :";

Echo $ db-> f ("Intro ");

Echo "<br> 〉";

Echo "<a href =" download. php? ID = ". $ db-> f (" ID ")." "> View word documents </a> 〉";

Echo "<br>
}

}

$ Db-> free ();

// Release resources

}

? > 〉

From the process above, we can see that the methods for accessing the database using a class are basically the same as those for accessing the database directly. The difference is that the methods we call here are class methods, rather than specific database functions. Due to the separation of code and specific database types, when the database system changes, we do not need to change the program code, as long as we need to change the implementation method of the base class.

If you use the PHPLib template for design, you can achieve the separation of the program and display. It will also make the program structure clear and make the webpage art easy to design and make.

Simple usage, reasonable task allocation, and appropriate object packaging will greatly improve the website development efficiency.

Appendix: code test platform
All the above code has passed the test on the following platform.

RedHat Linux 6.1 + Apache1.3.12 +

PHP4.0 + MySql3.22.32

The database installation and configuration process is as follows:

Cd/usr/local/src/mysql *

./Configure -- refix =/usr/local/mysql

Make

Make install

The installation and configuration process of Apache is as follows:

Cd/usr/local/src/apache *

./Configure -- prefix =/usr/local/apache -- enable-shared = max

Make

Make install

PHP installation and configuration process:

Cd/usr/local/src/php *

./Configure -- with-apxs =/usr/local/apache/bin/apxs

-- With-config-file-path =/usr/local/

Apache/conf

-- With-mysql =/usr/local/mysql

-- Enable-debug = no

-- Enable-track-vars

Php. ini configuration process:

Copy php. ini-dist to/usr/local/

Apache/conf/php. ini

Edit httpd. conf and remove the comments of the following two lines.

AddType application/x-httpd-php. php. php3

AddType application/x-httpd-php-source. phps

< <全文完> >

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.