Bluetom

php数据库连接操作类

db_host = $db_host;
$this->db_user = $db_user;
$this->db_password = $db_password;
$this->db_database = $db_database;
}
//连接数据库
function conn(){
$conn = mysql_connect($this->db_host,$this->db_user,$this->db_password);
if(!$conn){
$this->msg_error("无法连接数据库");
}else{
$this->conn = $conn;
}
if(!mysql_select_db($this->db_database,$this->conn)){
$this->msg_error("无法选择数据库");
}
return $this->conn;
}
//更新数据库,包括数据添加,更新
function query_noResult($sql){
if("" == $sql){$this->msg_error("传递了一个空变量");}
$this->sql = $sql;
//echo $this->sql;
//echo $this->conn;
if(!mysql_db_query($this->db_database,$this->sql,$this->conn)){
$this->msg_error(("数据更新过程发生错误!"));
}
return null;
}
//查询数据并返回result类型
function query_withResult($sql){
if("" == $sql){$this->msg_error("传递了一个空变量");}
$this->sql = $sql;
// $conn = mysql_connect("localhost","root","");
$result = mysql_db_query($this->db_database,$this->sql,$this->conn);
//一个字母错误浪费了两个小时!真不如java
//不过是写成了reslut

if(!$result){
$this->msg_error("数据查询过程发生错误或空记录!");
}else{
$this->result = $result;
}
//使用时,可以直接引用成员变量而无须关心返回值,除非需要返回值
//做更多处理
return $this->result;
}
//释放结果集
function free(){
mysql_free_result($this->result);
}
//关闭连接
function close(){
if($this->result){
$this->free();
}
mysql_close($this->conn);
}
// 根据执行结果取得影响行数
function db_affected_rows(){
return mysql_affected_rows();
}
// 根据查询结果计算结果集条数
function db_num_rows(){
if($this->result==null){
$this->msg_error("记录为空!");
}
return mysql_num_rows($this->result);
}
//取得记录集
function db_fetch_array(){
if($this->result==null){
//$this->msg_error("您查询的记录为空!");
}
$this->row = @mysql_fetch_array($this->result);
return $this->row;
}
//指向确定的一条数据记录
//通用数据库错误处理
function db_data_seek($result,$i){
mysql_data_seek($result,$i);
return $this->result;
}
function msg_error($message){
$time = date("Y年m月d日 H时i分s秒");
echo "很抱歉您操作数据库时发生了错误
";
echo "发生错误时间:".$time."
";
echo "错误信息:".$message."
";
echo "请发送邮件到aofanliguo@126.com..谢谢!";
// exit();
}
}
?>

具体使用:
sql="";
$this->db=$db;
}
//取得最新记录,可以更改sql语句得到完全控制
function fetch_new_all(){
$k=$this->random=Rand(4,10);
$f=$this->random-4;
$this->sql="select * from xl_news order by id desc limit ".$f.",".$k."";
$result=$this->db->query_withResult($this->sql);
$number=$this->db->db_num_rows();

for($i=0;$i<$number;$i++){ $row=mysql_fetch_array($result); $data[]=$row; } return $data; } //取得公告栏的数据并显示 function show_gonggao(){ $this->sql="select * from xl_gongao";
$result=$this->db->query_withResult($this->sql);
$row=mysql_fetch_array($result);
echo "最新更新:";
echo $row['date'];
echo "
";
echo $row['context'];
}
//提交咨询内容
function add_advisory($name,$sex,$age,$text){
$_name=$name;
$_sex=$sex;
$_age=$age;
$_text=$text;
$_date=date("Y-m-d H:i:s");
$this->sql="insert into xl_zixun(name,sex,age,context,date) values('$_name','$_sex','$_age','$_text','$_date')";
$this->db->query_noResult($this->sql);
}
}
?>

Bluetom

作为挨踢业的前段湿 搬过砖也画过画:爱看、爱听、爱玩儿、爱折腾、爱打撸啊撸、intj

Proudly published with Hexo