当前位置:首页 > PHP教程 > PHP基础知识

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

1 建立数据库表:
复制代码 代码如下:

create database club;
create table member(
id int(11) not null auto_increment,
no varchar(5) not null,
name varchar(10) not null,
age int(2) not null,
level varchar(10) not null,
sex tinyint(1) not null,
date datetime not null,
primary key(id)
)engine=myisam default charset=gb2312;
insert into member(id,no,name,age,level,sex,date)values
(1,'a001','wanxia',30,'hj',1,'2008-04-02 00:00:00'),
(2,'c022','liyan',29,'zs',1,'2007-05-31 00:00:00'),
(3,'a006','zhangyan',36,'hj',1,'2007-06-20 00:00:00'),
(4,'b052','luanying',42,'bj',1,'2007-02-12 00:00:00'),
(5,'a007','duxiang',26,'hj',2,'2008-03-26 00:00:00'),
(6,'c060','liuyu',38,'zs',1,'2008-10-16 00:00:00');


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

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312"/>
<title>会员列表</title>
</head>
<?php
$link=mysql_connect("localhost","root","123"); //连接mysql服务器
$db=mysql_select_db("club"); //选择数据库
mysql_query("set names utf8",$link); //设定编码方式
$sql="select * from member";
$result=mysql_query($sql,$link); //执行select查询
$num=mysql_num_rows($result); //获取记录查询
?>
<body>
<h1>健身俱乐部 会员名册</h1>
<br />
点击姓名可查看该会员详细资料,现有会员<?php echo $num ?>人。
<br />
<?php
if($num>0)
{
?>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>性别</td>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row['id']."</td><td><a href=member.php?name="
.$row['name'].">".$row['name']."</a></td><td>"
.($row['sex']==1?"女":"男")."</td></tr>";
}
?>
</table>
<?php
}
else
{
echo "俱乐部尚未发展会员。";
}
?>
</body>
</html>

2.2 建立member.php
复制代码 代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312"/>
<title>会员详细资料</title>
</head>
<?php
$link=mysql_connect("localhost","root","123"); //连接mysql服务器
$db=mysql_select_db("club"); //选择数据库
mysql_query("set names utf8",$link); //设定编码方式
$sql="select no,name,sex,age,level,date_format(date,'%y-%c-%d') as join_date from member "
."where name='".trim($_get['name'])."'";
$result=mysql_query($sql,$link); //执行在select查询
?>
<body>
<h1>健身俱乐部 会员详细资料</h1>
<?php
if($row=mysql_fetch_array($result))
{
echo "编号:".$row['no']."<br />";
echo "姓名:".$row['name']."<br />";
echo "性别:".($row['sex']==1?"女":"男")."<br />";
echo "年龄:".$row['age']."<br />";
echo "级别:".$row['level']."<br />";
echo "加入:".$row['join_date']."<br />";
}
?>
</body>
</html>


3 修改数据
3.1 建立level.php(修改数据)
复制代码 代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>俱乐部优惠活动</title>
</head>
<body>
<h1>俱乐部会员统计表</h1>
<?php
$link=mysql_connect("localhost","root","123"); //连接mysql服务器
$db=mysql_select_db("club"); //选择数据库
mysql_query("set name utf8",$link); //设定编码方式
$sql="select level,count(*) as num from member group by level";
$result=mysql_query($sql,$link); //执行select查询
while($row=mysql_fetch_array($result))
{
switch($row['level']){
case 'bj':
echo "等级:白金会员     人数:".$row['num']."<br />";
break;
case 'hj':
echo "等级:黄金会员     人数:".$row['num']."<br />";
break;
default:
echo "等级:钻石会员     人数:".$row['num']."<br />";
}
}
?>
<form action="up_level.php" name="level" method="post">
会员优惠升级:从
<select name="old_level">
<option value="hj">黄金会员</option>
<option value="bj">白金会员</option>
</select>
升级至
<select name="new_level">
<option value="bj">白金会员</option>
<option value="zs">钻石会员</option>
</select>
<input type="submit" value="确定"/>
</form>
</body>
</html>

3.2 建立up_level.php
复制代码 代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>俱乐部优惠活动</title>
</head>
<body>
<?php
$link=mysql_connect("localhost","root","123"); //连接mysql服务器
$db=mysql_select_db("club"); //选择数据库
mysql_query("set name utf8",$link); //设定编码方式
$sql="update member set level='".trim($_post['new_level'])
."' where level='".trim($_post['old_level'])."'";
$result=mysql_query($sql,$link); //执行select查询
echo mysql_affected_rows($link)."人 从";
switch(trim($_post['old_level'])){
case 'bj':
echo " 白金会员 " ;
break;
case 'hj':
echo " 黄金会员 ";
break;
default:
echo " 钻石会员 ";
}
echo "成功升级到";
switch(trim($_post['new_level'])){
case 'bj':
echo " 白金会员 " ;
break;
case 'hj':
echo " 黄金会员 ";
break;
default:
echo " 钻石会员 ";
}
?>
</body>
</html>


 
4 添加数据
4.1 建立add_member.php

复制代码 代码如下:

<html>
<meta http-equiv="content-type" content="text/html;charset=gb2312"/>
<title>新增会员</title>
<body>
<h1>新加入会员</h1>
<form action="newmember.php" method="post" name="add_member">
编号:<input type="text" name="no" width="40"/><br />
姓名:<input type="text" name="name" width="40"/><br />
性别:
<input type="radio" name="sex" value="1" />女
<input type="radio" name="sex" value="2" />男<br />
年龄:<input type="text" name="age" width="40" /><br />
级别:
<select name="level">
<option value="hj">黄金会员</option>
<option value="bj">白金会员</option>
<option value="zs">钻石会员</option>
</select><br />
<input type="submit" value="确定" />
</form>
</body>
</html>

4.2 建立newmember.php
复制代码 代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>添加会员</title>
</head>
<body>
<?php
$link=mysql_connect("localhost","root","123"); //连接mysql服务器
$db=mysql_select_db("club"); //选择数据库
mysql_query("set names gb2312",$link); //设定编码方式
$sql="insert member(no,name,sex,age,level,date) values('"
.trim($_post['no'])."','".trim($_post['name'])."','"
.trim($_post['sex'])."','".trim($_post['age'])."','"
.trim($_post['level'])."',now())";
$result=mysql_query($sql,$link); //执行select查询
$m_id=mysql_insert_id($link); //得到新插入会员记录的id
if(trim($_post['level'])=="hj") //判断新会员优惠
{
$sql="update member set level='bj' where id='".$m_id."'";
$result=mysql_query($sql,$link); //执行会员升级优惠
$text="已享受优惠升级至白金会员。";
}
$sql="select *,date_format(date,'%y-%c-%d') as join_date from member "
."where id='".$m_id."'";
$result=mysql_query($sql,$link); //执行select查询
if($row=mysql_fetch_array($result))
{
echo "新会员资料:<br />";
echo "编号:".$row['no']."<br />";
echo "姓名:".$row['name']."<br />";
echo "性别:".($row['sex']==1?"女":"男"."<br />");
echo "年龄:".$row['age']."<br />";
echo "级别:".$row['level']."<br />";
echo "加入:".$row['join_date']."<br />";
}
echo "新会员".$row['name']."添加成功".$text;
?>
</body>
</html>


 
5 创建类数据库连接
5.1 建立cls_mysql.php类文件

复制代码 代码如下:

<?php
class cls_mysql
{
protected $link_id;
function __construct($dbhost,$dbuser,$dbpw,$dbname='',$charset='gb2312')
{
if(!($this->link_id=mysql_connect($dbhost,$dbuser,$dbpw)))
{
$this->errormsg("can't pconnect mysql server($dbhost)!");
}
mysql_query("set names ".$charset,$this->link_id);
if($dbname)
{
if(mysql_select_db($dbname,$this->link_id)===false)
{
$this->errormsg("can't slect mysql database($dbname)!");
return false;
}
else
{
return true;
}
}
}
public function select_database($dbname)
{
return mysql_select_db($dbname,$this->link_id);
}
public function fetch_array($query,$result_type=mysql_assoc)
{
return mysql_fetch_array($query,$result_type);
}
public function query($sql)
{
return mysql_query($sql,$this->link_id);
}
public function affected_rows()
{
return mysql_affected_rows($this->link_id);
}
public function num_rows($query)
{
return mysql_num_rows($query);
}
public function insert_id()
{
return_insert_id($this->link_id);
}
public function selectlimit($sql,$num,$start=0)
{
if($start==0)
{
$sql.=' limit '.$num;
}
else
{
$sql.=' limit '.$start.', '.$num;
}
return $this->query($sql);
}
public function getone($sql,$limited=false)
{
if($limited=true)
{
$sql=trim($sql.' limit 1');
}
$res=$this->query($sql);
if($res!=false)
{
$row=mysql_fetch_row($res);
return $row[0];
}
else
{
return false;
}
}
public function getall($sql)
{
$res=$this->query($sql);
if($res!==false)
{
$arr=array();
while($row=mysql_fetch_assoc($res))
{
$arr[]=$row;
}
return $arr;
}
else
{
return false;
}
}
function errormsg($message='',$sql='')
{
if($message)
{
echo "<b> error info</b>:$messagenn";
}
else
{
echo "<b>mysql server error report:";
print_r($this->error_message);
}
exit;
}
}
?>

5.2 建立test.php
复制代码 代码如下:

<?php
include("cls_mysql.php");
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>mysql类库测试</title>
</head>
<body>
<?php
$sql="select * from member";
$db=new cls_mysql('localhost','root','123','club','gb2312');
$result=$db->selectlimit($sql,'3'); //从数据库中返回3个会员资料
if($result)
{
while($row=$db->fetch_array($result))
{
echo "会员编号: " .$row['no'].",姓名:".$row['name']."<br />";
}
}
?>
</body>
</html>


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值


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!