php封装一个类能实现mysql数据库的增删改查
按照下面的代码继续的完善publicfunctionmysql_connect(){if($this->links=@mysql_connect($this->host,...
按照下面的代码继续的完善
public function mysql_connect(){ if($this->links=@mysql_connect($this->host,$this->user,$this->pwd)){ if(@mysql_select_db($this->dbName,$this->links)){ $this->getCode(); }else{ $this->getMysqlError(); } }else{ $this->getMysqlError(); } } public function getMysqlError(){ echo "连接错误信息是".mysql_error(); echo "<br/>数据库错误信息是".mysql_errno(); } public function getCode(){ if(!empty($code)){ $this->code = $code; } mysql_query("set names".$this->code); } /* select() $can :结果集 */ public function select($res = null){ if(is_resource($res) && !is_null($res)){ $this->res = $res; } $data =array(); while($list=@mysql_fetch_assoc($this->res)){ $data[]=$list; } print_r($data); return $data; } /* find() $can :结果集 */ public function find($res = null){ if(is_resource($res) && !is_null($res)){ $this->res = $res; } return @mysql_fetch_assoc($this->res); }//增删改¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ //析构函数 public function __destruct(){ mysql_close($this->links); }} 展开
public function mysql_connect(){ if($this->links=@mysql_connect($this->host,$this->user,$this->pwd)){ if(@mysql_select_db($this->dbName,$this->links)){ $this->getCode(); }else{ $this->getMysqlError(); } }else{ $this->getMysqlError(); } } public function getMysqlError(){ echo "连接错误信息是".mysql_error(); echo "<br/>数据库错误信息是".mysql_errno(); } public function getCode(){ if(!empty($code)){ $this->code = $code; } mysql_query("set names".$this->code); } /* select() $can :结果集 */ public function select($res = null){ if(is_resource($res) && !is_null($res)){ $this->res = $res; } $data =array(); while($list=@mysql_fetch_assoc($this->res)){ $data[]=$list; } print_r($data); return $data; } /* find() $can :结果集 */ public function find($res = null){ if(is_resource($res) && !is_null($res)){ $this->res = $res; } return @mysql_fetch_assoc($this->res); }//增删改¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ //析构函数 public function __destruct(){ mysql_close($this->links); }} 展开
4个回答
展开全部
还没到20个问题吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
class mySql{
const HOST = "localhost";
const USER = "root";
const PWD = "123456";
const DBNAME = "thinklwt";
const CODE = "utf8";
public function __construct() {//封装数据库
$this->connect();
}
//链接数据库
public function connect(){
$link = @mysql_connect(self::HOST, self::USER, self::PWD);
if(!is_resource($link)){
echo $this->getError();
}
$db = mysql_select_db(self::DBNAME,$link);
if(!$db){
echo $this->getError();
}
mysql_query("set names ".self::CODE);
//echo '链接成功';
}
//增加数据
public function add($tname,$date){
$sql = "insert into " .$tname." set ";
foreach ($date as $key=>$vo){
$sql.=$key."='".$vo."',";
}
$sql = substr($sql,0,-1);
mysql_query($sql);
if(mysql_affected_rows()>0){
echo mysql_insert_id();
}else{
echo $this->getError();
}
}
public function getError(){
return mysql_errno().':'.mysql_error();
}
}
$db=new mySql();
$date = array("name"=>"lili","age"=>"20","sex"=>"男");
$db->add("st_user",$date);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-21
展开全部
看着好乱啊 !
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询