Home > Backend Development > PHP Tutorial > Detailed explanation of PHP script database functions (Part 1)_PHP tutorial

Detailed explanation of PHP script database functions (Part 1)_PHP tutorial

WBOY
Release: 2016-07-21 16:03:59
Original
855 people have browsed it

In the current situation where the Internet is developing rapidly and e-commerce websites are emerging one after another, higher and higher requirements are put forward for the efficiency and quality of website development.

For large-scale websites with complex structures and extensive content, it is necessary to achieve dynamic and convenient management of the website. Data management is inseparable from the support of database systems. An important indicator of measuring a CGI language is its ability to access the backend database, efficiency, etc.

And the new features of the currently popular PHP scripting language 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, templates, XML support, etc. have been used to bring new methods of website development. In terms of language structure, PHP has a structure similar to the C++ language, and introduces the concept of classes to simplify development.

PHP also has powerful database support capabilities. Here we will use examples to first introduce the general process of PHP accessing the database, and then introduce an advanced application of PHP accessing the database through database storage of files. Finally, through the use examples of the database class, a truly practical and efficient database development method is introduced.


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, etc. It is precisely because of the extensive database support that the application scope of PHP has been expanded, allowing various applications to be developed using PHP.

Among various databases, MySQL has been widely used due to its free, cross-platform, easy to use, and high access efficiency. Many central websites use the best combination of PHP+MySQL.

Oracle is a typical large-scale database application system. If the website you design has a large amount of data and high performance and efficiency requirements, Oracle is a good choice.

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

PHP encapsulates various database access methods, and the functions for different database systems are also very similar, increasing the convenience of use.

Next, we will use a simple talent information exchange center (see Figure 1) as an example to program the online submission and browsing of personal resumes and describe the entire process of PHP database operation. The database uses the most commonly used MySQL database.

Basic steps for PHP database operation
We will create a database named ResumeDB on the local machine. There is a table named Resume in the database. The table stores resume numbers, personnel names, personal profiles, and resume files in Word format.

1. Creation of database

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

./mysqladmin-u root-p create ResumeDB

Enter password:

Enter the password when prompted. If this is the first time the database is used, the default password is blank, just press Enter.

Then 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)

);

Place it in 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 of longbolb type and is used to store binary Word documents.

 2. Database access process

PHP’s access to the database generally includes the following steps: connect to the database system → select the database → execute the SQL statement → close the result set → close the database connection → end.

 (1) Connecting to the database

 Connecting to the database is the process of establishing a dialogue channel from the user program to the database system. The statement to connect to the MySQL database is as follows:

  ?

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

 ?〉

  Among them, the function mysql_connect() is used to establish a connection with the database server. The three parameters are: the host name of the database server (it can also be 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 a machine name that is not the local machine as the database server. This provides the possibility for off-site storage of data and safe isolation of the database. External users often have direct access rights to the WWW server. If the database system is placed directly on the WWW server, it may cause security risks.And if the database system is placed on a computer behind a firewall, PHP can access the database through the LAN, and the computer inside the LAN is invisible to the outside. In this way, the database is protected from external attacks.

The "@" symbol in front of the function is used to limit the display of error messages for this command. If an error occurs in the function call, the statement following or will be executed. The die() function means that the program terminates execution after outputting the content in quotation marks to the user. This is done to prevent users from seeing a bunch of inexplicable professional terms when a database connection error occurs, and instead prompts customized error messages. However, when debugging, we still can not block the error message, so as not to find out where the problem is 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 are going to use. The command to select a database is as follows:

 〈?

  @mysql_select_db("ResumeDB",$LinkID) or die("Error selecting database, maybe the database you specified does not exist!");

 ?〉

When selecting a database, the parameters to be provided are the name of the database and the server connection number.

When we only use one database server, $LinkID can be omitted, and the system automatically finds the nearest database connection and uses it. However, when you want to implement a large-scale site, you must encounter a multi-host and multi-database system. At this time, the database connection parameters cannot be omitted.

  (3) Database access

Well, we have established a connection to the database, selected the database, and the next thing is to 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 to the database and then display it.

 〈?

 $Name= "OpenBall"; //In actual operation, $Name and $Intro are the values ​​passed from the browser form

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

 $query = "insert into Resume(Name,Intro) values('$Name', '$Intro')"; //Generate SQL statement

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

 if(! $result)

  echo "Data insertion failed!";

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

$result = mysql_query($query,$LinkID); //Execute and save the result set to the variable $result in

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

 if($num == 0)

 {

echo "No records found";

exit();

}

while($row=mysql_fetch_array($result)) //Get the result set The next row of data is put into the array $row

 {

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

 //Access the value of the array variable based on the field name index

 }

 ?〉

  The above operation , a total of two database operations were performed. The first time is an insertion operation, and the second time is a query operation. The program first inserts the current user's one-day record, and then displays all personal situations in the database.

  (4) Resource release

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

 〈?

@mysql_free_result($result);

@mysql_close($LinkID); If multiple web pages require frequent database access, a continuous connection to the database server can be established to improve efficiency. Because each connection to the database server takes a long time and requires a large resource overhead, continuous connections are relatively more effective.

The way to establish a continuous connection is to call the function mysql_pconnect() instead of mysql_connect() when connecting to the database. The established continuous connection does not need to be closed by calling mysql_close() when this program ends. The next time the program executes mysql_pconnect() here, the system will automatically directly return the ID number of the established continuous connection without actually connecting to the database.




http://www.bkjia.com/PHPjc/316152.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/316152.htmlTechArticleIn the current situation where the Internet is developing rapidly and e-commerce websites are emerging one after another, the efficiency and quality of website development have been put forward to improve the efficiency and quality of website development. There are higher and higher demands. For large and complex structures and complex content...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template