程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP的數據庫增刪改查源代碼(1)

PHP的數據庫增刪改查源代碼(1)

編輯:PHP綜合

//mysql數據庫的sql
CREATE TABLE user( id int(5) DEFAULT '0' NOT NULL AUTO_INCREMENT, name varchar(20), city varchar(20), postcode varchar(10),address varchar(100), password varchar(10), level tinyint(2) DEFAULT '0',right tinyint(2),email varchar(40), PRIMARY KEY (id), UNIQUE id (id));
INSERT INTO myuser VALUES (1,'王友','北京','100083','北京市海澱區學院路',111,0,0,'[email protected]');
INSERT INTO myuser VALUES (2,'金風','武漢','430071','武漢市武昌區水果湖',111,0,0,'[email protected]');
INSERT INTO myuser VALUES (3,'常麗','襄樊','441021','襄樊市襄城區南街',111,0,0,'[email protected]');

1.查尋全部

<html>
<head>
 <title> 查尋全部</title>
</head>
<body>
 <h2 align=center>
 下面是用戶數據庫的所有內容:
 </h2>
<?
  $db = MySQL_connect("localhost", "root");
  MySQL_select_db("mysite",$db);
  $result = MySQL_query("SELECT * FROM user",$db);
  echo "<table border=1>\n";
  echo "<tr><td>id</td><td>姓名</td>";
  echo "<td>城市</td><td>郵編</td>";
  echo "<td>住  址</td><td>等級</td>";
  echo "<td>權限</td><td>郵件地址</td>";
  echo "</tr>\n";
while ($myrow = MySQL_fetch_row($result))
  {
   printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
   printf("<td>%s</td><td>%s</td>", $myrow[2], $myrow[3]);
   printf("<td>%s</td><td>%s</td>", $myrow[4], $myrow[6]);
   printf("<td>%s</td><td>%s</td></tr>\n", $myrow[7], $myrow[8]);
  }
echo "</table>\n";
?>
</body>
</html>

2.根據id查詢

<html>
<body>
 <title>
   根據id查詢
 </title>
<?
$db = MySQL_connect("localhost", "root");
MySQL_select_db("mysite",$db);
if ($id) {
  $result = MySQL_query("SELECT * FROM user WHERE id=$id",$db);
  $myrow = MySQL_fetch_array($result);
  printf("<h2 align=left>用戶的情況</h2>");
  printf("1.ID號 %sn<br>", $myrow["id"]);
  printf("2.姓名: %sn<br>", $myrow["name"]);
  printf("3.城市: %sn<br>", $myrow["city"]);
  printf("4.郵編: %sn<br>", $myrow["postcode"]);
  printf("5.住址: %sn<br>", $myrow["address"]);
  printf("6.等級:%sn<br>", $myrow["level"]);
  printf("7.郵件地址: %sn<br>", $myrow["email"]);
  printf("<a href="%s"> 繼續查詢 </a>", $PATH_INFO);
}
else
 {
  $result = MySQL_query("SELECT * FROM user",$db);
  if ($myrow = MySQL_fetch_array($result))
  {
   printf("<h2 align=left>請選擇要查詢的用戶:</h2>");
   do {
    printf("%s<a href="%s?id=%s">%s </a><br>\n",
    $myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);
     }
   while ($myrow = MySQL_fetch_array($result));
  }
  else
  {
   echo "對不起,沒有數據!";
  }
 }
?>
</body>
</html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved