A detailed description of the PHP scripting Database (top)

Source: Internet
Author: User
Keywords A detailed description of the PHP scripting Database (top)
In the current rapid development of the Internet, e-commerce sites in endless situation, the Web site development efficiency and quality has put forward more and more high requirements.

For large-scale and complex structure, the content of a wide range of sites, to achieve the dynamic and convenient site management. Data management is inseparable from database system support. And the important symbol of measuring a CGI language is its access to the background database, efficiency and so on.

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

PHP also has a strong database support capability. In this case, we will first introduce the general process of PHP access to the database, and then through the file database storage to introduce PHP access to the database of an advanced application. Finally, a practical and efficient database development method is introduced by using examples of database classes.


Figure 1

Introduction to PHP Database features
PHP provides support for more than 10 common databases, such as Oracle, DBase, Informix, SQL Server, Sysbase, MySQL, and more. It is because of extensive database support that it expands the scope of application of PHP so that various applications can be developed using PHP.

In various databases, MySQL has been widely used because of its free, cross-platform, easy to use and high access efficiency. Many central sites use Php+mysql as the best partner.

Oracle is a typical large database application system. Oracle is a good choice if you're designing a Web site with large data volumes, high performance and efficiency requirements.

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

PHP encapsulates the access methods of various databases, which are similar to the functions of different database systems, and increase the convenience of use.

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

Basic Steps for PHP database operation
We will create a database named Resumedb on the local machine, which is known as the resume table in the database. The table stores the number of resumes, the name of the person, the profile, and the CV file in Word format.

1. Creation of the database

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 the prompt. If the database is used for the first time, the default password is empty and the direct carriage return is possible.

Then create a table that holds your CV.

Create a text file Resume.sql with the following content:

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 the 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. Among them, the Resufile field is the Longbolb type, used to store the binary Word document.

2. Database access Process

PHP access to a database typically includes the following steps: Connecting to a database system → selecting a database → executing SQL statements → Closing the result set → closing the database connection → end.

(1) Connect to the database

Connecting a database is the process of establishing a conversation channel between a user program and a database system. The following statements connect 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 user name 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 of the database server (or IP), the database user name, 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 the local machine as the database server. In this way, it is possible to store the data offsite and secure the database isolation. Outside users often have the direct access to WWW server, if the database system is placed directly 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 LAN, 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 there is an error in the function call, the statement after or is executed. The Die () function indicates that the program terminates execution after outputting the contents of the quotation marks to the user. This is done to prevent database connection errors when the user sees a bunch of confusing professional nouns, but instead prompts for a custom error message. However, when debugging, we can still 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 command to select a database is as follows:

〈?

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

?〉

When you select a database, the parameters you provide are the name of the database, and the server connection number.

When we use only one database server, $LinkID can omit, the system automatically finds a recent database connection and then uses it. However, when you want to implement a large site, it is necessary to encounter multi-host, multi-database system situation. At this point, the database connection parameters cannot be omitted.

(3) Database access

OK, we have established a connection to the database, the database is selected, and then everything is executed SQL statements. The easy-to-use and powerful features of SQL statements will accomplish most of our database operations.

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

〈?

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

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

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

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

if (! $result)

echo "Data insertion failed! ";

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

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

$num = mysql_num_rows ($result); Gets the number of record rows returned by the query

if ($num = = 0)

{

echo "did not find any records";

Exit ();

}

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

{

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

Accessing the value of an array variable as a field name index

}

?〉

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

(4) Resource release

Finally, the result set is freed, and the result set and database connection resources are freed.

〈?

@mysql_free_result ($result);

@mysql_close ($LinkID);

?〉

If you have frequent database access across multiple Web pages, you can establish a continuous connection with the database server to improve efficiency. Because each connection to the database server takes a long time and a large resource overhead, persistent connections are relatively more efficient.

The method of establishing a persistent connection is to call the function mysql_pconnect () instead of mysql_connect () when the database is connected. The persistent connection established does not need to call Mysql_close () to close at the end of the program. The next time the program executes Mysql_pconnect (), the system automatically returns the ID number of the persistent connection that was 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.