亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP 新手入門資料庫表的操作

php ?增加

用於在資料庫表格中新增記錄

語法:

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

註:table_name ??表名??values(值)

接下來我們寫範(fàn)例解析

<?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ù)庫失敗');
		mysql_set_charset('utf8');
		$sql = "insert into user(`username`,`password`) values('$username','$password')";
		$info = mysql_query($sql);
		if($info){
			echo "添加成功";
		}else{
			echo "添加失敗";
		}

?>

註:首先要連結(jié)資料庫,然後判斷是否成功連結(jié)上

寫入增加的sql語句? $username ? $password ?為變量,是你要加入資料庫的值?

#然後執(zhí)行sql語句,判斷是否添加成功!最後我們要進(jìn)入資料庫表中查看,看看資料是否加入了


刪除



##DELETE FROM 語句用於從資料庫表中刪除記錄

#語法: delete ?from 表名where ?條件#程式碼如下:

<?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ù)庫失敗');
	mysql_set_charset('utf8');
	
	$sql = "delete from user where id = $id";
	$info = mysql_query($sql);

	if($info){
		echo "刪除成功";
	}else{
		echo "刪除失敗";
	}

?>
注意:刪除是需要有條件的,資料庫表有很多訊息,到底刪除那一條呢?

????????所以我們通常刪除的時候就會把id獲取到,然後根據(jù)id來刪除數(shù)據(jù),因?yàn)閕d是唯一的,用戶的名字有可能是相同的

#修改


#UPDATE 語句用於中修改資料庫表中的資料
語法:

UPDATE table_name SET column_name = new_value

WHERE column_name = some_value


#實(shí)例:

#

<?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ù)庫失敗');
	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 "修改失敗";
	}
?>

註:修改也是要帶id的,這樣才能知道修改哪一個資料username ?password ? 這是資料庫中的欄位

$username $password ? 這是你要輸入的內(nèi)容,這樣會把原來的內(nèi)容替換掉

查詢

查詢語句

select

語句用於從資料庫中選取資料

語法:SELECT column_name(s) FROM table_name

#SQL 語句對大小寫不敏感。 SELECT 與 select 等效。 為了讓PHP 執(zhí)行上面的語句,我們必須使用mysql_query() 函數(shù)

上節(jié)講函數(shù)的時候,其實(shí)我們已經(jīng)用到查詢語句了

#接下來看幾個案例:

實(shí)例:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>數(shù)據(jù)表操作    查詢</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫失敗');
			mysql_set_charset('utf8');
			$sql = "select * from user";  //查詢數(shù)據(jù)庫user這張表的所有內(nèi)容
			$info = mysql_query($sql);  //執(zhí)行sqL語句

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

	?>
</body>
</html>

註:查詢表中所有的,把他輸出出來

根據(jù)條件查詢

格式:select ?* from ?user where (條件) ;########################################################### ###實(shí)例:###
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>數(shù)據(jù)表操作  條件查詢</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫失敗');
			mysql_set_charset('utf8');

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

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

	?>
</body>
</html>
###註:這樣就會吧我們資料表中id是2的資料查詢並輸出出來################### ##取資料庫的2個資訊##########
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>數(shù)據(jù)表操作    查詢</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫失敗');
			mysql_set_charset('utf8');


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

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

	?>
</body>
</html>

注意

大家可能在limit1,2上面犯迷糊

這個1代表是從第幾條開始取,2是取多少條

排序:

查詢的時候是要把資料顯示出來,例如id ??有1到1000,這樣有1000筆數(shù)據(jù),在頁面顯示的時候,一定是id ?越大,內(nèi)容才更新,所以這個時候我們就要用到排序

預(yù)設(shè)是升序的, 倒序?order by ?id ??desc

#升序?asc

這句是根據(jù)id 來進(jìn)行倒序

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>數(shù)據(jù)表操作    查詢</title>
</head>
<body>
	<?php
			
			$con = mysql_connect('localhost','root','root') or die('連接服務(wù)器失敗');
			mysql_select_db('php') or die('連接數(shù)據(jù)庫失敗');
			mysql_set_charset('utf8');


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

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

	?>
</body>
</html>

注意:以上程式碼大家複製到本地進(jìn)行測試

繼續(xù)學(xué)習(xí)
||
<?php echo "歡迎學(xué)習(xí)增刪改查"; ?>
提交重置程式碼