PHP script database functions (part 1)

Source: Internet
Author: User
Tags php database
PHP script database features (I), read the PHP script database features (I), in the current rapid development of the Internet, the emergence of e-commerce websites, it puts forward higher and higher requirements on the efficiency and quality of website development. For large websites with complex structures and rich content, dynamic and convenient website management must be implemented. Data management is not "> <LINKhref =" http://www.php100.com in the current rapid development of the Internet, the emergence of e-commerce websites, the efficiency and quality of website development put forward more and more high requirements.

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. An important indicator of a CGI language 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 design and development in an object-oriented manner. At the same time, in order to meet the unique needs of web pages, templates and XML support have brought new methods for website development. 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 introduce the general process of accessing the database through PHP through an example, and then introduce an advanced application of accessing the database through PHP through the file database storage. Finally, we will introduce practical and efficient database development methods through Database-type use instances.




PHP database features
PHP supports more than 10 common databases, such as Oracle, dBase, Informix, SQL Server, Sysbase, and MySQL. It is precisely because of extensive database support that PHP applications can be expanded and various applications can be developed using PHP.

Among various databases, MySQL is widely used because it is free, cross-platform, easy to use, and highly efficient to access. 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 a large data volume 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, increasing ease of use.

Next, we will use a simple talent Information Exchange Center (see) as an example to program online submission, browsing, and other features of my resume, and describe the entire PHP database operation process. 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. The numbers, names, profiles, and Word-format resumes are stored in the table.

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, which is used to store 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 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 statement for connecting to the MySQL database is as follows:

<〈?

$ LinkID = @ mysql_connect ("localhost", "root", "") or die ("cannot connect to the database server! 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 the database server host name (or IP address), database user name, and user password. The return value of the function indicates 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, it is possible to store data in different regions and secure database isolation. External users often have direct access permissions to the WWW Server. If the database system is directly placed on the WWW server, security risks may occur. If you place the database system on a computer behind a firewall, PHP can access the database through the LAN, while computers inside the Lan are 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 a function call error occurs, the statement after or is executed. The die () function indicates that the program is terminated after the quotation marks are output to the user. To prevent database connection errors, you can see a bunch of inexplicable professional terms, but prompt custom error information. However, during debugging, we can 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 the system which database we will use. The command for selecting a database is as follows:

<〈?

@ Mysql_select_db ("ResumeDB", $ LinkID) or die ("An error occurred while selecting the database. It may be that 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.

When we only use one database server, $ LinkID can be omitted. The system automatically finds the most recent database connection and uses it. However, when you want to implement a large site, it is essential to encounter multiple hosts and multiple database systems. The database connection parameters cannot be omitted.

(3) database access

Now, we have established a connection to the database, and selected the database. All we need to do is execute the SQL statement. 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

$ 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. The program first inserts a one-day record of the current user, and then displays 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 to improve efficiency. Because each connection to the database server takes a long time and a large amount of resource overhead, the continuous connection is more effective.

The method for establishing a persistent connection is to call the mysql_pconnect () function to replace mysql_connect () during database connection (). When the program ends, you do not need to call mysql_close () to close the established persistent connection. The next time the program executes mysql_pconnect (), the system automatically returns the ID number of the established persistent connection instead of 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.