PHP Beginner's Introduction to Database Table Operations

php Add

Used to add new records to the database table

Syntax:

INSERT INTO table_name VALUES ( value1, value2,....);

Note: table_name table name values(value)

Next let’s write an example to analyze

<?php
		header("Content-type: text/html; charset=utf-8");//设置编码 
		$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
		mysql_select_db('php') or die('连接数据库失败');
		mysql_set_charset('utf8');
		$sql = "insert into user(`username`,`password`) values('$username','$password')";
		$info = mysql_query($sql);
		if($info){
			echo "添加成功";
		}else{
			echo "添加失败";
		}

?>

Note: First connect to the database, and then determine whether the connection is successful.

Write the added sql statement $username $password as a variable, which is the value you want to add to the database

Then execute the sql statement , determine whether the addition is successful! Finally, we need to enter the database table to see if the data has been added


Delete

DELETE FROM statement is used to delete records from the database table

Syntax: delete from table name where conditions

The code is as follows:

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
	mysql_select_db('php') or die('连接数据库失败');
	mysql_set_charset('utf8');
	
	$sql = "delete from user where id = $id";
	$info = mysql_query($sql);

	if($info){
		echo "删除成功";
	}else{
		echo "删除失败";
	}

?>

Note: Deletion requires conditions. There is a lot of information in the database table. Which one should be deleted?

So we usually get the id when deleting, and then delete the data based on the id, because the id is unique and the user’s name may be the same


Modify

##The UPDATE statement is used to modify data in the database table

Syntax:

UPDATE table_name SET column_name = new_value

WHERE column_name = some_value

Example:

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
	mysql_select_db('php') or die('连接数据库失败');
	mysql_set_charset('utf8');

	$username = $_POST['username'];
	$password = $_POST['password'];

	$sql = "update user set username = '$username',password='$password' where id = '$id'";

	$info = mysql_query($sql);
	if($info){
		echo "修改成功";
	}else{
		echo "修改失败";
	}
?>

Note: Modifications also require an ID, so that you know which data to modify username password This is a field in the database

$username $password This is what you want to enter Content, this will replace the original content


Query

Query statement

select

The statement is used to select data from the database

Syntax: SELECT column_name(s) FROM table_name

SQL statements are case-insensitive sensitive. SELECT is equivalent to select.

In order for PHP to execute the above statement, we must use the mysql_query() function

When we talked about functions in the previous section, we actually used the query statement

Next Look at a few cases:

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>数据表操作    查询</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
			mysql_select_db('php') or die('连接数据库失败');
			mysql_set_charset('utf8');
			$sql = "select * from user";  //查询数据库user这张表的所有内容
			$info = mysql_query($sql);  //执行sqL语句

			while($row = mysql_fetch_row($info)){
				echo "<pre>";
				print_r($row);
				echo "</pre>";
			}

	?>
</body>
</html>

Note: Query all the items in the table and output them


Query based on conditions

Format: select * from user where (condition);

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>数据表操作  条件查询</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
			mysql_select_db('php') or die('连接数据库失败');
			mysql_set_charset('utf8');

			$sql = "select * from user where id=2";  //查询数据库user这张表id是2的内容
			$info = mysql_query($sql);  //执行sqL语句

			while($row = mysql_fetch_row($info)){
				echo "<pre>";
				print_r($row);
				echo "</pre>";
			}

	?>
</body>
</html>

Note: This will query and output the data with id 2 in our data table


Get 2 pieces of information from the database

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>数据表操作    查询</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
			mysql_select_db('php') or die('连接数据库失败');
			mysql_set_charset('utf8');


			$sql = "select * from user limit 1,2";  //查询数据库user这张表的所有内容
			$info = mysql_query($sql);  //执行sqL语句

			while($row = mysql_fetch_row($info)){
				echo "<pre>";
				print_r($row);
				echo "</pre>";
			}

	?>
</body>
</html>

Attention

Everyone may be confused about limit1 and 2

This 1 represents which item to start from, 2 How many

are taken to sort:

When querying, the data must be displayed. For example, the id ranges from 1 to 1000, so there are 1000 When a piece of data is displayed on the page, the content must be updated as the id is larger, so at this time we have to use sorting

The default is ascending order, reverse order order by id desc

Ascending order asc

This sentence is to perform reverse order based on id

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>数据表操作    查询</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('连接服务器失败');
			mysql_select_db('php') or die('连接数据库失败');
			mysql_set_charset('utf8');


			$sql = "select * from user order by id desc";  //查询数据库user这张表的所有内容
			$info = mysql_query($sql);  //执行sqL语句

			while($row = mysql_fetch_row($info)){
				echo "<pre>";
				print_r($row);
				echo "</pre>";
			}

	?>
</body>
</html>

Note: Please copy the above code locally for testing

Continuing Learning
||
<?php echo "欢迎学习增删改查"; ?>
submitReset Code