PHP ???? ?? ?????? ??? ?? ??
?????? ???? ? ???? ???? ??
? ???????.
??:
INSERT INTO table_name VALUES( value1, value2,....);
??: table_name ??? ?? ?(value)
???? ?? ??? ?????
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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 "添加失敗"; } ?>
??: ?? ??????? ??? ? ?? ?? ??? ?????
??? sql ? $username $password? ??? ???? ??????? ????? ?? ?????
??? sql ?? ???? ?? ?? ??? ??????! ?????, ???? ?????? ???? ?? ?????? ???? ???? ???.
??
DELETE FROM ?? ?????? ????? ???? ???? ? ?????.
??: ??? ?? ??? ???? ??
??
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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 "刪除失敗"; } ?>
??: ???? ??? ?????. ?????? ????? ?? ??? ???? ????
??? ?? ??? ?? ???? ????, ? ???? ???? ???? ???? ??? ????. ???? ???? ????, ??? ??? ?? ? ?? ?????.
??
UPDATE ?? ?????? ???? ???? ???? ? ?????.
??:
UPDATE table_name SET ?? ?? = ?_?
WHERE ?? ?? = some_value
?:
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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 ????? ?????. , ?? ??? ?????
??
?? ?
select
?? ???????? ???? ???? ? ?????.
??: SELECT column_name(s) FROM table_name
SQL ?? ????? ?????. SELECT? ??? ?????.
PHP? ? ???? ????? mysql_query() ??? ???? ???
?? ???? ??? ?? ???? ? ???? ?? ?? ???? ??????
?? ? ?? ??? ???????.
?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>數(shù)據(jù)表操作 查詢</title> </head> <body> <?php $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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>
??: ?? ???? ?? ??? ?????
?? ?? ??
??: ????? * ?? where(??);
?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>數(shù)據(jù)表操作 條件查詢</title> </head> <body> <?php $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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('連接服務器失敗'); 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>
??
?? 1? 2? ?? ??? ? ????
? 1? ??? ??, 2? ? ??? ?????.
? ?????? ??:
??? ? ???? ????? ???. ?? ?? id? 1?? 1000?????. 1000?? ???? ???? ???? ID? ??? ??? ?????? ??? ?? ??? ???? ???
???? ????, ?????. id desc
???? asc
? ??? id
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>數(shù)據(jù)表操作 查詢</title> </head> <body> <?php $con = mysql_connect('localhost','root','root') or die('連接服務器失敗'); 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>
? ???? ??? ???? ?????. ??: ???? ?? ? ??? ??? ?????.