PHP Script Database function detailed (bottom) _php Foundation

Source: Internet
Author: User
Tags php database php script
Using classes to speed up PHP database development
The database has many access functions, and improper use can reduce the efficiency and even cause errors. and PHP itself is open and extensible, many people for it to develop a variety of functional source code. Every PHP programmer should be good at inheriting the results of others and saving time and effort. Standing on the shoulders of giants to see farther. Of course, you can also share your code and experience the sense of accomplishment of your work being recognized and created value.

The use of database classes allows us to focus on the development of a program without having to consider the specific database type at all.

Many development kits, Phplib is a more stable performance, the function of a more perfect one. Phplib can be obtained in http://phplib.netuse.de/. It contains the support classes for the database. Take the MySQL database as an example, Phplib with a class named Db_sql. It wraps the database connection, query, fetch result, database table traversal and so on function.

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

〈?

Require "db_mysql.php";

Build files that contain database classes

$db =new Db_sql;

Declaring instances of a database class

$db-〉connect ("resumedb", "localhost", "root", "");

Connecting to a database server

The parameters provided are: Database name, host name, username, user password

if ($db-〉link_id)

Determine if the connection is properly established

{

$db-〉query ("Select Id,name,intro from Resume");

Inquire

if ($db-〉nf ())

Determine if the result set is empty

{

while ($db-〉next_record ())

Gets the next line of record value until the recordset is finished

{

echo "ID:", $db-〉f ("id"); The f () function returns the value of a child segment of the current record

echo "〈br〉";

echo "Name:";

$db-〉p ("Name");

The P () function prints the value of a child segment directly

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 document 〈/a〉 ";

echo "〈br〉〈hr〉";

}

}

$db-〉free ();

Releasing resources

}

?〉

From the above process, we can see that the method of accessing the database by class is basically the same as that of accessing the database directly. The difference is that the method we call here is the method of the class, not the function specific to some kind of database. Because of the separation of the code and the specific database type, we do not need to change the program code when the database system changes, as long as we change the implementation method of the base class.

If the combination of the use of phplib template design, you can achieve the separation of the program and display. It will also make the structure of the program clear, Web page art design easy to make.

The simple usage, the reasonable task assignment, conforms to the thought the object packing, will make the website development efficiency greatly enhances.

Attached: Code test Platform
All the above program code is tested through the platform below

RedHat Linux 6.1+apache1.3.12+

php4.0+mysql3.22.32

The installation configuration process for the database is:

cd/usr/local/src/mysql*

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

Make

Make install

The Apache installation configuration process is:

cd/usr/local/src/apache*

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

Make

Make install

The installation configuration process for PHP is:

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

The php.ini configuration process is:

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

Apache/conf/php.ini

Edit the httpd.conf and remove the comments from the two lines below

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

AddType Application/x-httpd-php-source. Phps

<< finished full >>


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.