Home  >  Article  >  Backend Development  >  Detailed description of PHP combined with mysql

Detailed description of PHP combined with mysql

韦小宝
韦小宝Original
2018-03-09 11:33:576180browse

I believe that the databases used by many PHP programmers in PHP development are mysql databases. Why should PHP development be combined with mysql databases? Isn’t it possible to use other databases? Why not? Then let’s work together Let’s take a look and elaborate on the combination of PHP and MySQL and why PHP development must be combined with MySQL.

Recommended related mysql video tutorials: "mysql tutorial"                                               ​ ​ ​

Part 1: My application tools.

phpstudy: This package integrates the latest Apache+PHP+MySQL+phpMyAdmin. It can be installed once and can be used without configuration. It is a very convenient and easy-to-use PHP debugging environment. You no longer need to download and install php, mysql, and appche separately, it is simple and convenient.
First get to know the following phpstudy:
Detailed description of PHP combined with mysql

Click "MySQL Manager" in the picture above and select MySQL-Front
Detailed description of PHP combined with mysql

The following interface appears:
After opening, you will enter the formal database interface.
Detailed description of PHP combined with mysql
This is just for everyone to see the following mysql, and its specific operations will be discussed later.

In this part, the main problem I encountered was that I could not connect to mysql when opening the localhost in Figure 5. I chose to reinstall phpstudy because there was really no other way.

Part 2: Understand the relationship between php, apache, and mysql.

A customer purchased a book from an online store. After receiving the demand, the online store owner will go to the manufacturer to get the goods. The store owner will get the goods and send them to the customer.
The flow chart is as follows:

Detailed description of PHP combined with mysql
We know from the above picture that customers and store owners do not communicate directly, but exchange data through the store owner, who provides communication services. The relationship between php, apache, and database is like the relationship between customers, shop owners, and manufacturers. PHP needs some kind of data and sends the request directly to the apache server. apache then feeds the request back to the database. The database takes out the response data and gives it to apache The server, apache server then sends it to PHP.
1. Why can't PHP and the database communicate directly and have to go through apache?

For example: PHP and database are like two people of different nationalities. The former is from China and the latter is from the United States. If the language is not clear, communication will be a problem, let alone Talk about doing business. There happens to be a person named apache who understands both Chinese and English. He translates PHP's requirements into English and tells the database. The database takes out the corresponding goods and hands them to Apache, who then hands them to PHP.
php language and database data cannot recognize each other and need to be converted through apache.
2. Why can’t apache be both a server and a database? This is less Wouldn't it be more troublesome to have one link?
This is designed to solve the problem of rational division of labor.
If you merge apache with the database, it will be equivalent to apache being both a store owner and a manufacturer, which will greatly increase the workload of the apache server. When the data is small, Apache can still bear it. When the data is large, Apache has to bear the transportation and management work of the factory warehouse. The final efficiency may be greatly reduced. It is better to leave the warehouse transportation and management work to the factory.

mysql database(database)
There are many databases in the world, and mysql is one of the most popular.
MySQL is a relational database management system developed by the Swedish MySQL AB company and currently belongs to Oracle. MySQL is a relational database management system. A relational database stores data in different tables instead of putting all data in one large warehouse, which increases speed and flexibility. (Mysql database is equivalent to classifying goods and placing them in different warehouses. Each warehouse is a small database, and the factory is a large database. This makes it convenient to access goods. If the goods are not classified and stored, it will be very difficult. Obviously the storage efficiency will be greatly reduced).

Characteristics of RDBMS (Relational Database Management System):
1. Data appears in the form of tables
2. Each row has various record names
3. Each column is the data field corresponding to the record name
4. Many rows and columns form a form
5. Several forms form a database

Part 3: Using php to operate mysql database

1. Use php script Connecting to the database

PHP provides the mysql_connect() function to connect to the database. This function has 5 parameters, generally we only use the first 3.

这里你也许会问,php可以连接数据库那appache干什么去了。
这里说明一下,apache是一个服务器,其构筑了一个php和数据库可以沟通的环境.php和数据库这两个讲不同语言的人打电话,
通过apache这个同声翻译系统进行沟通

. server specifies the server to connect to. Can include a port number, such as "hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.
If the PHP directive mysql.default_host is not defined (the default), the default value is 'localhost:3306'.
. user User name. The default value is the username of the server process owner.
. password Password. The default value is an empty password.

php mysql_close is used to disconnect from the mysql database. Under normal circumstances, the script will automatically disconnect after running, so this sentence does not need to be written. However, mysql_close() does not close a persistent connection established by mysql_pconnect().
Connect to the database:

<?php
header("content-type:text/html;charset=utf-8");
$servername = "127.0.0.1";//服务器主机地址
$username = "root";//访问数据库的用户名
 $password = "root";//访问数据库的密码
 //phpstudy的mysql有一个默认用户名(root)和密码(root)

// 创建连接
$conn = mysql_connect($servername, $username,$password);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());//die()输出一条消息,然后断开
连接。
}
echo "连接成功";
mysql_close($conn);//关闭连接。
?>

Not everyone can access the database, only those with permission can access it. Next I will demonstrate how to add users using the mysql-front tool.

Detailed description of PHP combined with mysql
After adding a new user:

Detailed description of PHP combined with mysql
Use the new user to access the operation:

<?php
header("content-type:text/html;charset=utf-8");
$servername = "127.0.0.1";//服务器主机地址
$username = "sunwukong";//访问数据库的用户名
 $password = "jingubang";//访问数据库的密码
 //phpstudy的mysql有一个默认用户名(root)和密码(root)

// 创建连接
$conn = mysql_connect($servername, $username,$password);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());//die()输出一条消息,然后断开连接。
}
echo "连接成功";
mysql_close($conn);//关闭连接。
?>

The connection is successful.

2, Create database

php uses mysql_query (sql, connection) to create or delete a database. mysql_query() sends the search query statement sql to the database through the established connection connection. After reading the database, respond to the corresponding operation.

<?php
header("content-type:text/html;charset=utf-8");
$servername = "127.0.0.1";//服务器主机地址
$username = "sunwukong";//访问数据库的用户名
 $password = "jingubang";//访问数据库的密码
 //phpstudy的mysql有一个默认用户名(root)和密码(root)

// 创建连接
$conn = mysql_connect($servername, $username,$password);

// 检测连接
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());//die()输出一条消息,然后断开连接。
}
echo "连接成功";
echo "</br>";
$sql = "CREATE DATABASE zhubajie";//CREATE DATABASE是sql语言中创建数据库的语句,这里是一个创建名为“zhubajie”
的数据库的字符串。
$dingpa = mysql_query($sql,$conn);//将$sql查询语句发送给数据库。
if (!$dingpa) {
    die("创建数据库失败:".mysql_error());
}
echo "创建成功";
mysql_close($conn);//关闭连接。
?>

After running,
Detailed description of PHP combined with mysql

Sometimes you will find that the above php file will display the error shown in the figure below:

Detailed description of PHP combined with mysql
The reason for this error is that you access the database through the $username = “sunwukong”; username, and this user was not granted permissions when it was created in mysql.

Detailed description of PHP combined with mysql

3. Deleting the database

Deleting the database and creating the database are also implemented through mysql_query.

Detailed description of PHP combined with mysql

4. Create data table data

Detailed description of PHP combined with mysql

We will create a table named "MyGuests" with 5 Columns: "id", "firstname", "lastname", "email" and "reg_date":
Create the data table MyGuests:

CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)

is displayed in the data table as follows:

1Detailed description of PHP combined with mysql

Here we should pay attention to the header row in the table displayed by the object browser, which specifies the type of data and other attributes.

Detailed description of PHP combined with mysql
#The above picture is a specific data chart.
Let’s look at the specific code below:

In the early version of PHP we use the MySQL extension. However, this extension was deprecated starting in 2012. Instead, mysqli extension and PDO are used. Personally, I use the mysqli extension. The mysqli extension is an extension of the mysql extension. There is no difference between the two in use, but the former is more powerful than the latter. The following uses mysqli's process-oriented (it also has an object-oriented encoding) encoding method to operate mysql. You can compare it with the MySQL extended encoding method above.

1Detailed description of PHP combined with mysql

1Detailed description of PHP combined with mysql

AUTO INCREMENT - Set the value of the MySQL field to automatically increase by 1 each time a new record is added
PRIMARY KEY - Set the unique identifier of each record in the data table. Typically the column's PRIMARY KEY is set to the ID value, used with AUTO_INCREMENT. Each table should have a primary key (this column is the "zhubajie_t" column), and the primary key must contain a unique value. (You may not understand this well, but that will come later)

NOT NULL - Each row must contain a value (cannot be empty), and null values ​​are not allowed.
DEFAULT value - set the default value
UNSIGNED - Use unsigned numeric types, 0 and positive numbers
The above three attributes can be added to the column as well as the data type of the specified column. (Let’s understand it carefully here).

5. Delete table data

Detailed description of PHP combined with mysql

In fact, PHP combined with mysql development has long been a habit, just like men and women, Of course there are special circumstances.

Related recommendations:

Optimization summary of mysql database

With MySQL For example, let me help you understand those database military regulations

The above is the detailed content of Detailed description of PHP combined with mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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