Home >Backend Development >PHP Tutorial >14 new features added to ios8 Detailed explanation of PHP script database functions (Part 1)
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.
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 first introduce the general process of PHP accessing the database through examples, and then introduce an advanced application of PHP accessing the database through database storage of files. Finally, through usage examples of database classes, 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 the access methods of various databases, and the functions for different database systems are also very similar, increasing the convenience of use.
Next, we will take a simple talent information exchange center (see Figure 1) as an example to program to realize 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. Database creation
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 after the prompt . 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 the 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's 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! The database server may not be started, or the username and password are not valid Error! ");
?
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. In this way, it 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 information 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, you must provide The parameters 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 will automatically find the nearest database connection and use 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
Okay, 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 into 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); //Execution, result set Save to variable $result
$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 next row of data in the result set into the array $row
{
Echo $row["ID"]." ".$row["Name"] "." The first time is an insert 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 database access is required frequently in 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 requires a large resource overhead, continuous connections are relatively more effective.
The method to establish a continuous connection is to call the function mysql_pconnect() instead of mysql_connect() when connecting to the database. The established persistent 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.
The above has introduced the 14 new functions of ios8 and a detailed explanation of the PHP script database function (Part 1), including the 14 new functions of ios8. I hope it will be helpful to friends who are interested in PHP tutorials.