PHP 新手入門之?dāng)?shù)據(jù)庫表的操作
php ?增加
用于向數(shù)據(jù)庫表添加新記錄
語法:
INSERT INTO table_name??VALUES (value1, value2,....);
注:table_name ??表名 ??values(值)
接下來我們寫個(gè)例子解析
<?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 "添加失敗"; } ?>
注:首先要鏈接數(shù)據(jù)庫,然后判斷是否成功鏈接上
寫增加的sql語句 ? $username ? $password ?為變量,是你要添加到數(shù)據(jù)庫的值 ?
然后執(zhí)行sql語句,判斷是否添加成功!最后我們要進(jìn)入數(shù)據(jù)庫表中查看,看看數(shù)據(jù)是否添加進(jìn)來了
刪除
DELETE FROM 語句用于從數(shù)據(jù)庫表中刪除記錄
語法: 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 "刪除失敗"; } ?>
注意:刪除是需要有條件的,數(shù)據(jù)庫表有很多信息,到底刪除那一條呢?
????????所以我們通常刪除的時(shí)候就會(huì)把id獲取到,然后根據(jù)id來刪除數(shù)據(jù),因?yàn)閕d是唯一的,用戶的名字有可能是相同的
修改
UPDATE 語句用于中修改數(shù)據(jù)庫表中的數(shù)據(jù)
語法:
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的,這樣才能知道修改哪條數(shù)據(jù)username ?password ? 這是數(shù)據(jù)庫中的字段
$username $password ? 這是你要輸入的內(nèi)容,這樣會(huì)把原來的內(nèi)容替換掉
查詢
查詢語句
select
語句用于從數(shù)據(jù)庫中選取數(shù)據(jù)
語法:SELECT column_name(s) FROM table_name
SQL 語句對(duì)大小寫不敏感。SELECT 與 select 等效。
為了讓 PHP 執(zhí)行上面的語句,我們必須使用 mysql_query() 函數(shù)
上節(jié)講函數(shù)的時(shí)候,其實(shí)我們已經(jīng)用到查詢語句了
接下來看幾個(gè)案例:
實(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>
注:這樣就會(huì)吧我們數(shù)據(jù)表中id是2的數(shù)據(jù)查詢并輸出出來
取數(shù)據(jù)庫的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上面犯迷糊
這個(gè)1代表是從第幾條開始取,2是取多少條
排序:
查詢的時(shí)候是要把數(shù)據(jù)顯示出來,比如id ??有1到1000,這樣有1000條數(shù)據(jù),在頁面顯示的時(shí)候,肯定是id ?越大,內(nèi)容才更新,所以這個(gè)時(shí)候我們就要用到排序
默認(rèn)是升序的, 倒序 ?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>
注意:以上代碼大家復(fù)制到本地進(jìn)行測(cè)試