Maison >base de données >tutoriel mysql >mysql 类 待更新完善_MySQL

mysql 类 待更新完善_MySQL

WBOY
WBOYoriginal
2016-06-01 13:11:38780parcourir

<?phpclass Mysql{	//连接句柄	public $con = null;	public function __construct($host,$user,$pwd,$db)	{		$this->connect($host,$user,$pwd,$db);	}	//数据库连接	public function connect($host,$user,$pwd,$db)	{	    $this->con = @mysql_connect($host,$user,$pwd,true);		if(!$this->con)		{			echo '连接失败'.mysql_error();			die();		}		if(!mysql_select_db($db,$this->con))		{			echo '库选择失败'.mysql_error();		}		mysql_query('set names utf8');	}	//查询	Public function query($sql)	{		$rs = mysql_query($sql,$this->con);		if(!$rs)		{			echo 'sql语句错误'.mysql_error();			die();		}		return $rs;	}	//单挑查询	public function find($sql)	{		$rt = mysql_fetch_assoc($this->query($sql));		return $rt;	}	//所有查询	public function findall($sql)	{		$r = $this->query($sql);		$arr = array();		while(($row = mysql_fetch_assoc($r))!=false)		{			$arr[] = $row;		}		return $arr;	}	//查询记录数	public function count($sql)	{		$r = $this->query($sql);		return mysql_num_rows($r);	}	//更新数据	public function update($table,$data,$condition)	{		$sql = 'update '.$table.' set ';		while(list($k,$v) = each($data))		{			$sql .= $k.' = "'.$v.'"'.',';		}		$sql = substr($sql,0,-1);		$sql .=' where 1=1';		while(list($key,$val) = each($condition))		{			$sql .= ' and  '.$key.' = "'.$val.'"';		}		echo $sql;		$this->query($sql);	}	//删除数据	//condition array($key=>$val,.......)	Public function del($table,$condition)	{		//$condition = array('id'=>12,'name'=>'zj');		$sql = 'delete from '.$table.' where 1=1';		while(list($key,$val) = each($condition))		{			$sql .= ' and '.$key.' = "'.$val.'"';		}		$this->query($sql);	}}$db = new Mysql('192.168.0.230','root','fpdev','oa');//$one = print_r($db->find('select * from users'));//$all = print_r($db->findall('select * from users'));//echo $db->count('select * from users');//$db->del('user_bak',array('id'=>1019,'username'=>'katherine'));$db->update('user_bak',array('username'=>'zhangsan'),array('id'=>1020,'username'=>'lq'));


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:随手记mysql_MySQLArticle suivant:MySQL导入导出_MySQL