Article Directory
MySQL
- MySQL is the name of a database
- A better database that works with PHP
- The front end asks for data from the back end, and the back end queries the data in the database and returns it to the front end
- Next, let’s talk about using PHP to manipulate the database
MySQL 是最流行的关系型数据库管理系统(非关系型数据库简略介绍)
关系数据库管理系统(Relational Database Management System)的特点
1.数据以表格的形式出现
2.每行为各种记录名称
3.许多的行和列组成一张表单
4.若干的表单组成database
5.主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
Data type of the database
- Numerical type
Types of | size | description |
---|---|---|
tinyint | 1 byte | Small integer value |
smallint | 2 bytes | Large integer value |
mediumint | 3 bytes | Large integer value |
int or integer | 4 bytes | Large integer value |
bigint | 8 bytes | Very large integer value |
float | 4 bytes | Single-precision floating-point value |
double | 8 bytes | Double-precision floating-point value |
decimal | Decimal value |
- Date and time type
Types of | format | description |
---|---|---|
DATE (date) | YYYY-MM-DD | Date value |
TIME(time) | HH:MM:SS | Time value or duration |
YEAR(year) | YYYY | Year value |
DATETIME(datetime) | YYYY-MM-DD HH:MM:SS | Mixed date and time values |
TIMESTAMP(timestamp) | YYYYMMDD HHMMSS | Timestamp |
- String type
Types of | size | description |
---|---|---|
CHAR(char) | 0-255 bytes | Fixed-length string |
VARCHAR(varchar) | 0-65535 bytes | Variable length string |
TINYBLOB (tinyblob) | 0-255 bytes | Binary string of no more than 255 characters |
TINYTEXT (tinytext) | 0-255 bytes | Short text string |
BLOB(blob) | 0-65 535 bytes | Long text data in binary form |
TEXT(text) | 0-65 535 bytes | Long text data |
MEDIUMBLOB(mediumblob) | 0-16 777 215 bytes | Medium-length text data in binary form |
MEDIUMTEXT(mediumtext) | 0-16 777 215 bytes | Medium-length text data |
LONGBLOB(longblob) | 0-4 294 967 295 bytes | Very large text data in binary form |
LONGTEXT (longtext) | 0-4 294 967 295 bytes | Very large text data |
PHP operating database
- Previously, it was a simple understanding of the database, you don’t need to memorize it all, just understand it.
- Next, we are to use php to link to the mysql database to add, delete, modify and check data
- To operate the database, in addition to the php syntax, a mysql sql statement is also required
- Steps to use php to manipulate the database
- Establish a link with the database
- Use sql statement to operate the database
- Get results
- The link to the database is broken
Establish a link with the database
- php5 syntax:
mysql_connect('ip地址','数据库用户名','数据库密码')
mysql_select_db('你要操作的小仓库名称')
- php7 syntax:
mysqli_connect('ip地址','数据库用户名','数据库密码','你要操作的小仓库名称')
<?php
$link = mysqli_connect('127.0.0.1','root','root','student');
?>
-我们有了链接信息`$link`以后就可以继续操作数据库了
Execute SQL statement
-
php5 syntax:
mysql_query('你要执行的sql语法',连接信息)
-
php7 syntax:
mysqli_query(连接信息,'你要执行的sql语法')
<?php
# 避免中文乱码
mysqli_query($link,"set names utf8");
# 下面就是使用SQL语句对数据库进行操作
$res = mysqli_query($link,"SELECT * FROM student");
?>
Analytical data
- php5 syntax:
解析本行数据:mysqli_fetch_assoc(要解析的数据);
没有解析全部结果的方法
- php7 syntax:
解析本行数据:mysqli_fetch_assoc(要解析的数据);
解析所有数据:mysqli_fetch_all(要解析的数据,MYSQLI_ASSOC);
<?php
# 解析所有行的数据
$arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
# 转json查看获取到的结果
print_r(json_encode($arr));
?>
Close link
-
After all is used up, we'd better close the database link
-
php5 syntax:
mysql_close(链接信息);
-
php7 syntax:
mysqli_close(链接信息);
<?php
mysqli_close($link);
?>
Complete steps
- Let's write the complete steps of the operation
<?php
$link = mysqli_connect('127.0.0.1','root','root','student');
mysqli_query($link,"set names utf8");
$res = mysqli_query($link,"SELECT * FROM student");
$arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
print_r(json_encode($arr));
mysqli_close($link);
?>
Commonly used SQL statements
- I just said how to operate the database
- Now we learn about SQL statements commonly used when operating a database
- We rely on these sql statements for database operations
check sentence
<?php
# 查询 student 这个表里面的所有数据
$sql = 'SELECT * FROM `student`';
# 查询 student 表中的数据里面 gender 为 男 的数据
$sql = 'SELECT * FROM `student` WHERE `gender`="男"';
# 查询 student 表中的数据里面 age 大于 18 的数据
$sql = 'SELECT * FROM `student` WHERE `age`>18';
# 查询 student 表中的数据里面 age 大于 18 且 gender 为 男 的数据
$sql = 'SELECT * FROM `student` WHERE `age`>18 AND `gender`="男"';
# 查询 student 表中的数据里面 age 小于 22 或者 age 大于 28 的数据
$sql = 'SELECT * FROM `student` WHERE `age`<22 OR `age`>28';
# 查询 student 表中的数据里面从 第几条开始 查询多少条
$sql = 'SELECT * FROM `student` LIMIT 0, 10';
# 先按照条件筛选出数据以后再进行分页查询
# 下面是查询表中所有 age>18 且 性别为男的所有数据,查出来以后从第 10 条开始查 10 条
$sql = 'SELECT * FROM `student` WHERE `age`>18 AND `gender`="男" LIMIT 10, 10';
# 查询表的模糊查询
# 下面表示查询表中所有数据里面 name 字段中包含 "三" 字的数据
$sql = 'SELECT * FROM `student` WHERE `name` LIKE "%三%"';
# 查询排序,查询的时候按照某一个字段升序或降序排序
$sql = 'SELECT * FROM `student` ORDER BY `age` ASC';
$sql = 'SELECT * FROM `student` ORDER BY `age` DESC';
?>
Add sentence
<?php
# 向表中增加一条数据,再增加的时候主键不能由我们书写,而是 mysql 数据库自己递增
$sql = 'INSERT INTO `student` VALUES(null, "张三", 18, "男", 1913, 100)';
# 插入固定几个键的数据,其他的用默认值
$sql = 'INSERT INTO `student` (`name`, `age`) VALUES("李四", 22)';
?>
Delete statement
<?php
# 删除表中 id 为 100 的数据
$sql = 'DELETE FROM `student` WHERE `id`=100';
# 删除表中 name 为 张三 的数据
$sql = 'DELETE FROM `student` WHERE `name`="张三"'
?>
Modify the statement
<?php
# 更新一条 id 为 100 的数据中的 name 字段的值和 age 字段的值
$sql = 'UPDATE `student` SET `name`="张三", `age`=10 WHERE `id`=100'
# 更新数据的时候让所有的数据增加一些内容
$sql = 'UPDATE `student` SET `age`=age+1'
?>