国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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");//設(shè)置編碼 
		$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
		mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
		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");//設(shè)置編碼 
	$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
	mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
	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");//設(shè)置編碼 
	$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
	mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
	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>數(shù)據(jù)表操作    查詢(xún)</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
			mysql_set_charset('utf8');
			$sql = "select * from user";  //查詢(xún)數(shù)據(jù)庫(kù)user這張表的所有內(nèi)容
			$info = mysql_query($sql);  //執(zhí)行sqL語(yǔ)句

			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>數(shù)據(jù)表操作  條件查詢(xún)</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
			mysql_set_charset('utf8');

			$sql = "select * from user where id=2";  //查詢(xún)數(shù)據(jù)庫(kù)user這張表id是2的內(nèi)容
			$info = mysql_query($sql);  //執(zhí)行sqL語(yǔ)句

			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>數(shù)據(jù)表操作    查詢(xún)</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
			mysql_set_charset('utf8');


			$sql = "select * from user limit 1,2";  //查詢(xún)數(shù)據(jù)庫(kù)user這張表的所有內(nèi)容
			$info = mysql_query($sql);  //執(zhí)行sqL語(yǔ)句

			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>數(shù)據(jù)表操作    查詢(xún)</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫(kù)失敗');
			mysql_set_charset('utf8');


			$sql = "select * from user order by id desc";  //查詢(xún)數(shù)據(jù)庫(kù)user這張表的所有內(nèi)容
			$info = mysql_query($sql);  //執(zhí)行sqL語(yǔ)句

			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 "歡迎學(xué)習(xí)增刪改查"; ?>
submitReset Code