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','2007-05-31 00:00:00'),
(3,'A006','zhangyan',36,'2007-06-20 00:00:00'),
(4,'B052','luanying',42,'bj','2007-02-12 00:00:00'),
(5,'A007','duxiang',26,2,'2008-03-26 00:00:00'),
(6,'C060','liuyu',38,'2008-10-16 00:00:00');
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','2007-05-31 00:00:00'),
(3,'A006','zhangyan',36,'2007-06-20 00:00:00'),
(4,'B052','luanying',42,'bj','2007-02-12 00:00:00'),
(5,'A007','duxiang',26,2,'2008-03-26 00:00:00'),
(6,'C060','liuyu',38,'2008-10-16 00:00:00');
2 读取数据
2.1 建立01.PHP
代码
代码如下:
<Meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
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); //获取记录查询
?>
健身俱乐部 会员名册
点击姓名可查看该会员详细资料,现有会员人。
PHP
if($num>0)
{
?>
}
else
{
echo "俱乐部尚未发展会员。";
}
?>
2.2 建立member.PHP
代码如下:
<Meta http-equiv="Content-Type" content="text/html;charset=GB2312"/>
PHP
$link=MysqL_connect("localhost",$link); //设定编码方式
$sql="select no,date_format(date,'%Y-%c-%d') as join_date from member "
."where name='".trim($_GET['name'])."'";
$result=MysqL_query($sql,$link); //执行在select查询
?>
健身俱乐部 会员详细资料
PHP
if($row=MysqL_fetch_array($result))
{
echo "编号:".$row['no']."
";
echo "姓名:".$row['name']."
";
echo "性别:".($row['sex']==1?"女":"男")."
";
echo "年龄:".$row['age']."
";
echo "级别:".$row['level']."
";
echo "加入:".$row['join_date']."
";
}
?>
3 修改数据
3.1 建立level.PHP(修改数据)
代码如下:
俱乐部会员统计表
PHP
$link=MysqL_connect("localhost","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']."
";
break;
case 'hj':
echo "等级:黄金会员 人数:".$row['num']."
";
break;
default:
echo "等级:钻石会员 人数:".$row['num']."
";
}
}
?>
3.2 建立up_level.PHP
代码如下:
PHP
$link=MysqL_connect("localhost",$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 " 钻石会员 ";
}
?>
代码如下:
4.2 建立newmember.PHP
代码如下:
PHP
$link=MysqL_connect("localhost","123"); //连接MysqL服务器
$db=MysqL_select_db("club"); //选择数据库
MysqL_query("set names GB2312",$link); //设定编码方式
$sql="Insert member(no,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 *,'%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 "新会员资料:
";
echo "编号:".$row['no']."
";
echo "姓名:".$row['name']."
";
echo "性别:".($row['sex']==1?"女":"男"."
");
echo "年龄:".$row['age']."
";
echo "级别:".$row['level']."
";
echo "加入:".$row['join_date']."
";
}
echo "新会员".$row['name']."添加成功".$text;
?>
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,$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 " error info:$message\n\n";
}
else
{
echo "MysqL server error report:";
print_r($this->error_message);
}
exit;
}
}
?>
class cls_MysqL
{
protected $link_id;
function __construct($dbhost,$dbuser,$dbpw,$dbname='',$charset='GB2312')
{
if(!($this->link_id=MysqL_connect($dbhost,$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 " error info:$message\n\n";
}
else
{
echo "MysqL server error report:";
print_r($this->error_message);
}
exit;
}
}
?>
5.2 建立test.PHP
代码如下:
PHP
include("cls_MysqL.PHP");
?>
MysqL类库测试
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']."
";
}
}
?>
include("cls_MysqL.PHP");
?>
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']."
";
}
}
?>
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值 原文链接:https://www.f2er.com/php/28705.html