Discuz教程网

PHP5与MySQL数据库操作常用代码 收集

[复制链接]
authicon dly 发表于 2011-9-5 16:58:45 | 显示全部楼层 |阅读模式
1 建立数据库表:
代码如下:

  1. create database club;
  2. create table member(
  3. id int(11) not null auto_increment,
  4. no varchar(5) not null,
  5. name varchar(10) not null,
  6. age int(2) not null,
  7. level varchar(10) not null,
  8. sex tinyint(1) not null,
  9. date datetime not null,
  10. primary key(id)
  11. )engine=MyISAM default charset=GB2312;
  12. insert into member(id,no,name,age,level,sex,date)values
  13. (1,'A001','wanxia',30,'hj',1,'2008-04-02 00:00:00'),
  14. (2,'C022','liyan',29,'zs',1,'2007-05-31 00:00:00'),
  15. (3,'A006','zhangyan',36,'hj',1,'2007-06-20 00:00:00'),
  16. (4,'B052','luanying',42,'bj',1,'2007-02-12 00:00:00'),
  17. (5,'A007','duxiang',26,'hj',2,'2008-03-26 00:00:00'),
  18. (6,'C060','liuyu',38,'zs',1,'2008-10-16 00:00:00');

复制代码


20100321153520830.jpg
2 读取数据
2.1 建立01.php
代码
代码如下:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
  4. <title>会员列表</title>
  5. </head>
  6. <?php
  7. $link=mysql_connect("localhost","root","123"); //连接mysql服务器
  8. $db=mysql_select_db("club"); //选择数据库
  9. mysql_query("set names utf8",$link); //设定编码方式
  10. $sql="Select * from member";
  11. $result=mysql_query($sql,$link); //执行select查询
  12. $num=mysql_num_rows($result); //获取记录查询
  13. ?>
  14. <body>
  15. <h1>健身俱乐部 会员名册</h1>
  16. <br />
  17. 点击姓名可查看该会员详细资料,现有会员<?php echo $num ?>人。
  18. <br />
  19. <?php
  20. if($num>0)
  21. {
  22. ?>
  23. <table border="1" cellpadding="1" cellspacing="1">
  24. <tr>
  25. <td>序号</td>
  26. <td>姓名</td>
  27. <td>性别</td>
  28. </tr>
  29. <?php
  30. while($row=mysql_fetch_array($result))
  31. {
  32. echo "<tr><td>".$row['id']."</td><td><a href=member.php?name="
  33. .$row['name'].">".$row['name']."</a></td><td>"
  34. .($row['sex']==1?"女":"男")."</td></tr>";
  35. }
  36. ?>
  37. </table>
  38. <?php
  39. }
  40. else
  41. {
  42. echo "俱乐部尚未发展会员。";
  43. }
  44. ?>
  45. </body>
  46. </html>
复制代码

2.2 建立member.php
代码如下:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
  4. <title>会员详细资料</title>
  5. </head>
  6. <?php
  7. $link=mysql_connect("localhost","root","123"); //连接mysql服务器
  8. $db=mysql_select_db("club"); //选择数据库
  9. mysql_query("set names utf8",$link); //设定编码方式
  10. $sql="select no,name,sex,age,level,date_format(date,'%Y-%c-%d') as join_date from member "
  11. ."where name='".trim($_GET['name'])."'";
  12. $result=mysql_query($sql,$link); //执行在select查询
  13. ?>
  14. <body>
  15. <h1>健身俱乐部 会员详细资料</h1>
  16. <?php
  17. if($row=mysql_fetch_array($result))
  18. {
  19. echo "编号:".$row['no']."<br />";
  20. echo "姓名:".$row['name']."<br />";
  21. echo "性别:".($row['sex']==1?"女":"男")."<br />";
  22. echo "年龄:".$row['age']."<br />";
  23. echo "级别:".$row['level']."<br />";
  24. echo "加入:".$row['join_date']."<br />";
  25. }
  26. ?>
  27. </body>
  28. </html>
复制代码

20100321153520531.jpg
20100321153520223.jpg
3 修改数据
3.1 建立level.php(修改数据)
代码如下:


  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=GB2312" />
  4. <title>俱乐部优惠活动</title>
  5. </head>
  6. <body>
  7. <h1>俱乐部会员统计表</h1>
  8. <?php
  9. $link=mysql_connect("localhost","root","123"); //连接mysql服务器
  10. $db=mysql_select_db("club"); //选择数据库
  11. mysql_query("set name utf8",$link); //设定编码方式
  12. $sql="Select level,count(*) as num from member group by level";
  13. $result=mysql_query($sql,$link); //执行select查询
  14. while($row=mysql_fetch_array($result))
  15. {
  16. switch($row['level']){
  17. case 'bj':
  18. echo "等级:白金会员 人数:".$row['num']."<br />";
  19. break;
  20. case 'hj':
  21. echo "等级:黄金会员 人数:".$row['num']."<br />";
  22. break;
  23. default:
  24. echo "等级:钻石会员 人数:".$row['num']."<br />";
  25. }
  26. }
  27. ?>
  28. <form action="up_level.php" name="level" method="post">
  29. 会员优惠升级:从
  30. <select name="old_level">
  31. <option value="hj">黄金会员</option>
  32. <option value="bj">白金会员</option>
  33. </select>
  34. 升级至
  35. <select name="new_level">
  36. <option value="bj">白金会员</option>
  37. <option value="zs">钻石会员</option>
  38. </select>
  39. <input type="submit" value="确定"/>
  40. </form>
  41. </body>
  42. </html>
复制代码
3.2 建立up_level.php
代码如下:


  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=GB2312" />
  4. <title>俱乐部优惠活动</title>
  5. </head>
  6. <body>
  7. <?php
  8. $link=mysql_connect("localhost","root","123"); //连接mysql服务器
  9. $db=mysql_select_db("club"); //选择数据库
  10. mysql_query("set name utf8",$link); //设定编码方式
  11. $sql="update member set level='".trim($_POST['new_level'])
  12. ."' where level='".trim($_POST['old_level'])."'";
  13. $result=mysql_query($sql,$link); //执行select查询
  14. echo mysql_affected_rows($link)."人 从";
  15. switch(trim($_POST['old_level'])){
  16. case 'bj':
  17. echo " 白金会员 " ;
  18. break;
  19. case 'hj':
  20. echo " 黄金会员 ";
  21. break;
  22. default:
  23. echo " 钻石会员 ";
  24. }
  25. echo "成功升级到";
  26. switch(trim($_POST['new_level'])){
  27. case 'bj':
  28. echo " 白金会员 " ;
  29. break;
  30. case 'hj':
  31. echo " 黄金会员 ";
  32. break;
  33. default:
  34. echo " 钻石会员 ";
  35. }
  36. ?>
  37. </body>
  38. </html>
复制代码

20100321153520635.jpg

20100321153520356.jpg
4 添加数据
4.1 建立add_member.php
代码如下:


  1. <html>
  2. <meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
  3. <title>新增会员</title>
  4. <body>
  5. <h1>新加入会员</h1>
  6. <form action="newmember.php" method="post" name="add_member">
  7. 编号:<input type="text" name="no" width="40"/><br />
  8. 姓名:<input type="text" name="name" width="40"/><br />
  9. 性别:
  10. <input type="radio" name="sex" value="1" />女
  11. <input type="radio" name="sex" value="2" />男<br />
  12. 年龄:<input type="text" name="age" width="40" /><br />
  13. 级别:
  14. <select name="level">
  15. <option value="hj">黄金会员</option>
  16. <option value="bj">白金会员</option>
  17. <option value="zs">钻石会员</option>
  18. </select><br />
  19. <input type="submit" value="确定" />
  20. </form>
  21. </body>
  22. </html>
复制代码
4.2 建立newmember.php
代码如下:


  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html;charset=GB2312" />
  4. <title>添加会员</title>
  5. </head>
  6. <body>
  7. <?php
  8. $link=mysql_connect("localhost","root","123"); //连接mysql服务器
  9. $db=mysql_select_db("club"); //选择数据库
  10. mysql_query("set names GB2312",$link); //设定编码方式
  11. $sql="Insert member(no,name,sex,age,level,date) values('"
  12. .trim($_POST['no'])."','".trim($_POST['name'])."','"
  13. .trim($_POST['sex'])."','".trim($_POST['age'])."','"
  14. .trim($_POST['level'])."',now())";
  15. $result=mysql_query($sql,$link); //执行select查询
  16. $m_id=mysql_insert_id($link); //得到新插入会员记录的id
  17. if(trim($_POST['level'])=="hj") //判断新会员优惠
  18. {
  19. $sql="Update member set level='bj' where id='".$m_id."'";
  20. $result=mysql_query($sql,$link); //执行会员升级优惠
  21. $text="已享受优惠升级至白金会员。";
  22. }
  23. $sql="Select *,date_format(date,'%Y-%c-%d') as join_date from member "
  24. ."where id='".$m_id."'";
  25. $result=mysql_query($sql,$link); //执行select查询
  26. if($row=mysql_fetch_array($result))
  27. {
  28. echo "新会员资料:<br />";
  29. echo "编号:".$row['no']."<br />";
  30. echo "姓名:".$row['name']."<br />";
  31. echo "性别:".($row['sex']==1?"女":"男"."<br />");
  32. echo "年龄:".$row['age']."<br />";
  33. echo "级别:".$row['level']."<br />";
  34. echo "加入:".$row['join_date']."<br />";
  35. }
  36. echo "新会员".$row['name']."添加成功".$text;
  37. ?>
  38. </body>
  39. </html>
复制代码

20100321153520928.jpg
20100321153520156.jpg

20100321153520391.jpg
5 创建类数据库连接
5.1 建立cls_mysql.php类文件
代码如下:


  1. <?php
  2. class cls_mysql
  3. {
  4. protected $link_id;
  5. function __construct($dbhost,$dbuser,$dbpw,$dbname='',$charset='GB2312')
  6. {
  7. if(!($this->link_id=mysql_connect($dbhost,$dbuser,$dbpw)))
  8. {
  9. $this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
  10. }
  11. mysql_query("SET NAMES ".$charset,$this->link_id);
  12. if($dbname)
  13. {
  14. if(mysql_select_db($dbname,$this->link_id)===false)
  15. {
  16. $this->ErrorMsg("Can't slect MYSQL database($dbname)!");
  17. return false;
  18. }
  19. else
  20. {
  21. return true;
  22. }
  23. }
  24. }
  25. public function select_database($dbname)
  26. {
  27. return mysql_select_db($dbname,$this->link_id);
  28. }
  29. public function fetch_array($query,$result_type=MYSQL_ASSOC)
  30. {
  31. return mysql_fetch_array($query,$result_type);
  32. }
  33. public function query($sql)
  34. {
  35. return mysql_query($sql,$this->link_id);
  36. }
  37. public function affected_rows()
  38. {
  39. return mysql_affected_rows($this->link_id);
  40. }
  41. public function num_rows($query)
  42. {
  43. return mysql_num_rows($query);
  44. }
  45. public function insert_id()
  46. {
  47. return_insert_id($this->link_id);
  48. }
  49. public function selectLimit($sql,$num,$start=0)
  50. {
  51. if($start==0)
  52. {
  53. $sql.=' LIMIT '.$num;
  54. }
  55. else
  56. {
  57. $sql.=' LIMIT '.$start.', '.$num;
  58. }
  59. return $this->query($sql);
  60. }
  61. public function getOne($sql,$limited=false)
  62. {
  63. if($limited=true)
  64. {
  65. $sql=trim($sql.' LIMIT 1');
  66. }
  67. $res=$this->query($sql);
  68. if($res!=false)
  69. {
  70. $row=mysql_fetch_row($res);
  71. return $row[0];
  72. }
  73. else
  74. {
  75. return false;
  76. }
  77. }
  78. public function getAll($sql)
  79. {
  80. $res=$this->query($sql);
  81. if($res!==false)
  82. {
  83. $arr=array();
  84. while($row=mysql_fetch_assoc($res))
  85. {
  86. $arr[]=$row;
  87. }
  88. return $arr;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. function ErrorMsg($message='',$sql='')
  96. {
  97. if($message)
  98. {
  99. echo "<b> error info</b>:$message\n\n";
  100. }
  101. else
  102. {
  103. echo "<b>MySQL server error report:";
  104. print_r($this->error_message);
  105. }
  106. exit;
  107. }
  108. }
  109. ?>
复制代码

5.2 建立test.php
代码如下:


  1. <?php
  2. include("cls_mysql.php");
  3. ?>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html;charset=GB2312" />
  7. <title>Mysql类库测试</title>
  8. </head>
  9. <body>
  10. <?php
  11. $sql="Select * from member";
  12. $db=new cls_mysql('localhost','root','123','club','GB2312');
  13. $result=$db->selectLimit($sql,'3'); //从数据库中返回3个会员资料
  14. if($result)
  15. {
  16. while($row=$db->fetch_array($result))
  17. {
  18. echo "会员编号: " .$row['no'].",姓名:".$row['name']."<br />";
  19. }
  20. }
  21. ?>
  22. </body>
  23. </html>
复制代码

20100321153520822.jpg
6 总结
6.1 mysql_connect():建立与MySQL服务器的连接
6.2 mysql_select_db():选择数据库
6.3 mysql_query():执行数据库查询
6.4 mysql_fetch_array():获取数据库记录
6.5 mysql_num_rows():获取查询得到的记录数
6.6 mysql_affected_rows():最近一次操作影响到的行数
6.7 mysql_insert_id():最近一次插入记录的ID值





上一篇:PHP一步一步学习(8) PHP 数组
下一篇:PHP中基本符号及使用方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 05:02

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表