Foreword: Relational database management systems are absolutely unavoidable in back-end development. Common database management systems include MySql
, SQLServer
, Oracle
, etc.
Mysql is the most commonly used database management system.
Recommended mysql related video tutorials: https://www.php.cn/course/list/51/type/4.html
I will introduce it to you today Check out Mysql.
I will summarize and organize relevant knowledge for you in the future, thank you!
Official website download address: https://dev.mysql.com/downloads/mysql/
Note: During the database installation process, we need to enable management You must have user rights to install, otherwise the installation will fail due to insufficient rights.
1. Create a data table
Syntax: create table table name (field 1, field 2...);
Use PHP script to create a data table:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //创建数据表 $sql = "CREATE TABLE demo02(id SMALLINT(10) PRIMARY KEY, age TINYINT(3), name VARCHAR(10))CHARSET=utf8"; $query = mysqli_query($conn, $sql); //判断是否创建成功 if(!$query) { die('创建失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
2. Delete the data table
Syntax: drop table table name;
Use PHP script to delete the data table:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //删除数据表 $sql = "DROP TABLE runoob_tbl"; $query = mysqli_query($conn, $sql); if(!$query) { die('删除失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
3. Insert data
Syntax: insert into table name (field 1, field 2 ...)values(value1,value2...);
Use PHP script to insert data:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //插入数据 $sql = "INSERT INTO demo02(id,age,name) VALUES(4,20,'mm')"; $query = mysqli_query($conn,$sql); //var_dump($query); //die(); //判断是否插入成功 if(!$query) { die('插入失败!'.mysqli_error($conn)); } //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
4. Query data
Syntax: select * from table;
Use PHP script to query data:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //判断连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //查询数据 $sql = "SELECT * FROM demo02"; $query = mysqli_query($conn, $sql); //判断是否查询成功 if(!$query) { die('查询失败!'.mysqli_error($conn)); } echo 'mysql查询语句'; echo '<table border=1>'; echo '<tr> <td>id</td> <td>age</td> <td>name</td> </tr>'; //循环数据 while($row = mysqli_fetch_assoc($query)) { echo '<tr> <td>'.$row['id'].'</td> <td>'.$row['age'].'</td> <td>'.$row['name'].'</td> </tr>'; } echo '</table>'; //释放内存 mysqli_free_result($query); //关闭连接 mysqli_close($conn); ?>
5, update statement
Syntax: update table name set field1 = new value;
Use PHP to update data
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.mysqli_connect_error()); } //更新数据 $sql = "UPDATE demo02 SET name='zylmy' WHERE id=1"; $query = mysqli_query($conn, $sql); //var_dump($query); //die(); //判断是否更新成功 if(!$query) { die('更新失败!'.mysqli_error($conn)); } //关闭连接 mysqli_close($conn); ?>
The above are some basic operations about mysql compiled for you. I hope it will be useful to you. Everyone helps.
For more information, please pay attention to the relevant tutorials on the php Chinese website.
Recommended related articles: https://www.php.cn/php-weizijiaocheng-428722.html
The above is the detailed content of Detailed introduction and examples of mysql operations (2). For more information, please follow other related articles on the PHP Chinese website!