PHP Script Database function detailed (above) _php Foundation

Source: Internet
Author: User
Tags php database php script
In the current rapid development of the Internet, E-commerce sites in the emerging situation, the Web site development efficiency and quality of more and more high requirements.

For large and complex structure, the content of a wide range of sites, to achieve the dynamic site and convenient management. Data management is inseparable from the support of database system. The important sign of a CGI language is its ability to access the background database and its efficiency.

and the current popular PHP scripting language, its new features bring us a new feeling. It supports design and development in an object-oriented manner. At the same time, in order to meet the unique needs of Web pages, using templates, XML support and so on to bring a new Web site development methods. In language architecture, PHP has a structure similar to C + + language, and introduces the concept of class, simplifying development.

PHP also has a strong database support capability. We have here through the example, first introduced PHP to access the general flow of the database, and then through the file database storage to introduce PHP access to a high-level application of the database. Finally, a practical and efficient database development method is introduced by using the example of database class.


Figure 1

Introduction to PHP Database functions
PHP provides support for more than 10 common databases, such as Oracle, DBase, Informix, SQL Server, Sysbase, MySQL, and so on. It is because of a wide range of database support, only to expand the scope of PHP application, so that all kinds of applications can be developed using PHP.

In all kinds of databases, MySQL is widely used because of its free, cross-platform, easy to use and high access efficiency. Many central Web sites use the best partner of Php+mysql.

Oracle is a typical large database application system. Oracle is a good choice if you design a large number of Web sites with high performance and efficiency requirements.

On the WIN32 platform, SQL Server occupies a larger market. PHP can access SQL Server.

PHP to the various database access methods encapsulation, for different database system functions are very similar to increase the ease of use.

Below, we will take a simple talent information exchange Center (see Figure 1) as an example, program to achieve the personal resume online submission, browsing and other functions, the whole process of PHP database operation. The database uses the most commonly used MySQL database.

PHP Database Operation Basic Steps
We will create a database called Resumedb on the local machine, a table named resume in the database. The table stores the profile number, person name, profile, and resume file in Word format.

1. Creation of databases

Switch to the/usr/local/mysql/bin directory, and at the command line, execute the following statement to create the database:

./mysqladmin-u Root-p Create Resumedb

Enter Password:

Enter the password after prompting. If the database is used for the first time, the default password is null and you can enter directly.

Then create a table that holds your resume.

Create a text file Resume.sql that reads as follows:

Use Resumedb;

CREATE TABLE Resume (

ID tinyint (4) not NULL auto_increment,

Name varchar (ten) is not NULL,

Intro varchar (255),

Resufile Longblob,

PRIMARY KEY (ID),

KEY ID (ID)

);

Place it under my executable directory/usr/local/mysql/bin and execute the following command:

./mysql-u Root-p〈resume.sql

Enter Password:

After entering the database password, the table resume is automatically created successfully. Where the Resufile field is a Longbolb type, used to store binary Word documents.

2. Database access Process

PHP access to the database typically includes the following steps: Connecting to the database system → select database → Execute SQL statement → close result set → close database connection → end.

(1) Connecting to the database

Connecting a database is the process of establishing a dialog channel for a user program to a database system. The following statements are attached to the MySQL database:

〈?

$LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!") The database server may not be started, or the username password is incorrect! ");

?〉

Where the function mysql_connect () is used to establish a connection to the database server. The three parameters are: the host name (or IP) of the database server, the database username, and the user password. The function return value is used to represent this database connection.

As you can see from this command, we can specify that the machine name is not native to the database server. In this way, it provides the possibility for the data offsite storage and database security isolation. Outside users often have the direct access of WWW server, if the database system directly placed on the WWW server, it may bring security risks. If the database system is placed on a computer behind a firewall, PHP can access the database over the local area network, and the computer inside the LAN is invisible to the outside. This ensures that the database is not subject to external attacks.

The "@" symbol in front of the function to limit the display of error messages for this command. If a function call fails, the statement following or is executed. The Die () function indicates that the program terminates execution after the contents of quotation marks are output to the user. This is done to prevent database connection errors, the user sees a bunch of inexplicable professional nouns, but prompts the custom error message. However, when debugging, we still can not block the error message, so that after the error, it is difficult to find out where there is a problem.

(2) Database selection

A database system can contain multiple databases. After establishing a connection to the database server, we will tell the system which database we are going to use. The commands for selecting a database are as follows:

〈?

@mysql_select_db ("Resumedb", $LinkID) or Die ("Select Database error, may be the database you specified does not exist!") ");

?〉

When you select a database, the parameter to provide is the name of the database, and the server connection number.

When we use only one database server, $LinkID can omit, the system automatically finds the closest database connection and then uses it. However, when you want to implement a large site, it is necessary to encounter the situation of multiple hosts, multiple database systems. At this point, the database connection parameters can not be omitted.

(3) Database access

OK, we've set up a connection to the database, we've selected the database, and all that's coming up is executing the SQL statement. The Easy-to-use and powerful features of SQL statements will complete most of our database operation actions.

We can first write a personal information record to the database and then show it.

〈?

$Name = "Openball"; In practice, $Name, $Intro is the value from the browser form

$Intro = "Openball's personal profile ...";

$query = "INSERT into Resume (Name,intro) VALUES (' $Name ', ' $Intro ')"; Generate SQL statements

$result = @mysql_query ("$query", $LinkID); Perform

if (! $result)

echo "Data insertion failed!" ";

$query = "Select Id,name,intro from Resume"; Generate SQL statements

$result = mysql_query ($query, $LinkID); execution, the result set is saved to the variable $result

$num = mysql_num_rows ($result); Get the number of rows returned by a query

if ($num = = 0)

{

echo "did not find any records";

Exit ();

}

while ($row =mysql_fetch_array ($result))//Take the next line of data from the result set into the array $row

{

echo $row ["ID"]. " ". $row [" Name]. " ". $row [" Intro "]." 〈br〉 ";

To access the value of an array variable with the field name index

}

?〉

The above operation, a total of two database operations performed. The first time for the insert operation, and the second for the query operation. The program inserts the current user's day record first, and then displays the personal information in all databases.

(4) Resource release

At the end of the operation, you release the result set, releasing the result set and the database connection resource.

〈?

@mysql_free_result ($result);

@mysql_close ($LinkID);

?〉

If you have frequent database access on multiple Web pages, you can build a continuous connection to the database server to improve efficiency. Because each connection to the database server takes a long time and a large resource overhead, a continuous connection is relatively effective.

The way to establish a continuous connection is to call the function mysql_pconnect () instead of mysql_connect () when the database is connected. The established continuous connection does not need to be called mysql_close () to close at the end of this program. The next time the program executes Mysql_pconnect (), the system automatically returns the ID number of the persistent connection that has been established, instead of actually connecting to the database.

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.