Home  >  Article  >  Database  >  Detailed introduction and examples of mysql operations (2)

Detailed introduction and examples of mysql operations (2)

王林
王林Original
2019-08-17 16:47:112145browse

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 = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.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(&#39;创建失败!&#39;.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 = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//删除数据表
$sql = "DROP TABLE runoob_tbl";
$query = mysqli_query($conn, $sql);
if(!$query) {
die(&#39;删除失败!&#39;.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 = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//插入数据
$sql = "INSERT INTO demo02(id,age,name)
VALUES(4,20,&#39;mm&#39;)";
$query = mysqli_query($conn,$sql);
//var_dump($query);
//die();
//判断是否插入成功
if(!$query) {
die(&#39;插入失败!&#39;.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 = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//判断连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//查询数据
$sql = "SELECT * FROM demo02";
$query = mysqli_query($conn, $sql);
//判断是否查询成功
if(!$query) {
die(&#39;查询失败!&#39;.mysqli_error($conn));
}
echo &#39;mysql查询语句&#39;;
echo &#39;<table border=1>&#39;;
echo &#39;<tr>
<td>id</td>
<td>age</td>
<td>name</td>
</tr>&#39;;
//循环数据
while($row = mysqli_fetch_assoc($query)) {
echo &#39;<tr>
<td>&#39;.$row[&#39;id&#39;].&#39;</td>
<td>&#39;.$row[&#39;age&#39;].&#39;</td>
<td>&#39;.$row[&#39;name&#39;].&#39;</td>
</tr>&#39;;
}
echo &#39;</table>&#39;;
//释放内存
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 = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//更新数据
$sql = "UPDATE demo02 SET name=&#39;zylmy&#39; WHERE id=1";
$query = mysqli_query($conn, $sql);
//var_dump($query);
//die();
//判断是否更新成功
if(!$query) {
die(&#39;更新失败!&#39;.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!

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