PHP script database Function Details _ MySQL

Source: Internet
Author: User
Tags php database
With the rapid development of the Internet and the endless emergence of e-commerce websites, website development efficiency and quality are increasingly demanding. For large websites with complex structures and rich content, dynamic and convenient website management must be implemented. Data management is inseparable from the support of the database system. A major indicator of a CGI language is that it is used for website development in the context of the rapid development of the Internet and the endless emergence of e-commerce websites.
Higher and higher requirements are put forward.

The dynamic and
Convenient management. Data management is inseparable from the support of the database system. The
An important indicator is its access capability and efficiency to the background database.

The popular PHP scripting language, its new features, brings us a new feeling. It supports object-oriented
Design and Development. At the same time, in order to meet the unique needs of web pages, templates and XML Support Bring New Website Development
Method. In terms of language structure, PHP has a structure similar to the C ++ language, introduces the concept of classes, and simplifies development.

PHP also has powerful database support capabilities. Here we will use an example to first introduce the general usage of PHP to access the database.
Process, and then introduce an advanced application for PHP to access the database through the file database storage. Finally
Use instances to introduce practical and efficient database development methods.



PHP database features
PHP supports more than 10 common databases, such as Oracle, dBase, Informix, SQL Server,
Sysbase, MySQL, etc. It is precisely because of extensive database support that PHP applications are expanded, making various applications
PHP can be used for development.

Among various databases, MySQL has gained a lot of benefits due to its free, cross-platform, convenient use, and high access efficiency.
Application. Many central websites use PHP + MySQL as the best partner.

Oracle is a typical large-scale database application system. If you design a website with large data volumes and high performance and efficiency requirements
Oracle is a good choice.

On the Win32 Platform, SQL Server occupies a large market. PHP can access SQL Server.

PHP encapsulates access methods for various databases, and functions for different database systems are similar.
Ease of use.

Next, we will use a simple talent Information Exchange Center (see) as an example to program
Online submission, browsing, and other functions, describes the entire process of PHP database operations. The database uses the most commonly used MySQL database.

PHP database operation steps
We will create a database named ResumeDB on the local machine. The database contains a table named Resume. Table storage items
The number, name, profile, and Word of a person's resume.

1. create a database

Switch to the/usr/local/mysql/bin directory, and run the following statement on the command line to create a database:

./Mysqladmin-u root-p create ResumeDB

Enter password:

Enter the password after the prompt. If the database is used for the first time and the default password is blank, press enter.

Create a table to save your resume.

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

Use ResumeDB;

Create table Resume (

ID tinyint (4) not null auto_increment,

Name varchar (10) not null,

Intro varchar (255 ),

ResuFile longblob,

Primary key (ID ),

Key id (ID)

);

Put it in My executable directory/usr/local/mysql/bin and execute the following command:

./Mysql-u root-p <Resume. SQL

Enter password:

After you enter the database password, the table Resume is automatically created. The ResuFile field is longbolb type, used to store
Stores binary word documents.

2. database access process

PHP generally includes the following steps to access the database: connect to the database system → Select database → execute SQL statements → Close
Close result Set → close Database Connection → end.

(1) connecting to the database

Database Connection is the process of establishing a dialog channel between user programs and the database system. The statements used to connect to the MySQL database are as follows:
Below:

<〈?

$ LinkID = @ mysql_connect ("localhost", "root", "") or die ("cannot connect to the database service
Tool! The database server is not started, or the user name and password are incorrect! ");

? > 〉

The mysql_connect () function is used to establish a connection with the database server. The three parameters are: Database
The host name (or IP address), database user name, and user password of the server. The return value of the function is used to indicate the database.
Connection.

From this command, we can specify a machine name that is not the local machine as the database server. In this way
Remote data storage and secure database isolation are possible. External users often have direct access to the WWW Server
Limit. if the database system is directly placed on the WWW server, it may bring security risks. However
Placed on a computer behind a firewall, PHP can access the database through the LAN, while the computer inside the LAN
Invisible to the outside. This ensures that the database is not subject to external attacks.

The "@" symbol before the function is used to limit the display of error information of this command. If the function call fails
Statement after line or. The die () function indicates that the program is terminated after the quotation marks are output to the user. This is
When a database connection error is prevented, the user will see a bunch of inexplicable professional terms, but will prompt the custom error information. However
During debugging, we can still avoid blocking error information, so that it is difficult to locate the problem after an error occurs.

(2) database selection

A database system can contain multiple databases. After establishing a connection with the database server, we need to tell
System, which database will we use. The command for selecting a database is as follows:

<〈?

@ Mysql_select_db ("ResumeDB", $ LinkID) or die ("An error occurred while selecting the database, which may be the number you specified
The specified database does not exist! ");

? > 〉

When selecting a database, the parameter to be provided is the database name and the connection number to the server.

$ LinkID can be omitted when only one database server is used. The system automatically searches for the latest database.
Connect and use it. However, when you want to implement a large site, it is essential to have multiple hosts and databases.
System. The database connection parameters cannot be omitted.

(3) database access

Well, we have established a connection to the database, and selected the database. All we need to do is execute the SQL language.
Sentence. The easy-to-use and powerful functions of SQL statements will complete most of our database operations.

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

<〈?

$ Name = "OpenBall"; // in actual operations, $ Name and $ Intro are values sent from the browser form.

$ Intro = "OpenBall profile ...... ";

$ Query = "insert into Resume (Name, Intro) values ('$ name',' $ intro')"; // Generate an SQL statement
Statement

$ Result = @ mysql_query ("$ query", $ LinkID); // execute

If (! $ Result)

Echo "data insertion failed! ";

$ Query = "select ID, Name, Intro from Resume"; // Generate an SQL statement

$ Result = mysql_query ($ query, $ LinkID); // run the command. The result set is saved to the variable $ result.

$ Num = mysql_num_rows ($ result); // gets the number of records returned by the query.

If ($ num = 0)

{

Echo "no records found ";

Exit ();

}

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

{

Echo $ row ["ID"]. "". $ row ["Name"]. "". $ row ["Intro"]. "<br> 〉";

// Use the field name as the index to access the value of the array variable

}

? > 〉

The preceding operations performed two database operations. The first is the insert operation, and the second is the query operation. Program first
Insert the one-day record of the current user, and then display the personal information in all databases.

(4) resource release

At the end of the operation, the result set is released, and the result set and database connection resources are released.

<〈?

@ Mysql_free_result ($ result );

@ Mysql_close ($ LinkID );

? > 〉

If frequent database access is required on multiple web pages, you can establish a continuous connection with the database server.
High efficiency. Because each connection to the database server takes a long time and a large amount of resource overhead
Is more effective.

The method for establishing a persistent connection is to call the mysql_pconnect () function to replace the database connection.
Mysql_connect (). When the program ends, you do not need to call mysql_close () to close the established persistent connection. Lower
When the program executes mysql_pconnect (), the system automatically returns the ID number of the established persistent connection instead
Connect to the database.


Original Author: Wang Kaibo
Source: CCID

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.