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>:$message\n\n";
}
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值