当前位置:编程学习 > php >>

在PHP设计数据库中如何增删改查源代码(上)

http://blog.csdn.net/newhappy2008/archive/2006/12/06/1432654.aspx

//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,'wangyou000@263.net');
INSERT INTO myuser VALUES (2,'金风','武汉','430071','武汉市武昌区水果湖',111,0,0,'jinfeng000@263.net');
INSERT INTO myuser VALUES (3,'常丽','襄樊','441021','襄樊市襄城区南街',111,0,0,'changli000@263.net');

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>

 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>

www.zzzyk.com,学习电脑知识的好地方

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,