Heim > Artikel > Backend-Entwicklung > 一个简单记事本php操作mysql辅助类创建
//SqlHelper.class.php<?phpclass SqlHelper{ public $conn; public $host="localhost"; public $user="root"; public $passwrd="passwd"; public $db="notebook"; function __construct(){ $this->conn=mysql_connect($this->host,$this->user,$this->passwrd); if(!$this->conn){ die("连接失败".mysql_error()); } mysql_select_db($this->db); mysql_query("set names utf8"); } //增删改 function execute_dml($sql){ $b=mysql_query($sql); if(!$b){ die("操作失败".mysql_error()); }else if(mysql_affected_rows($this->conn)>0){ return 1; }else{ return 2; } } //查询,多条结果 function execute_dql($sql){ $arr=array(); $res=mysql_query($sql); $i=0; while($row=mysql_fetch_assoc($res)){ $arr[$i++]=$row; } mysql_free_result($res); return $arr; } //查询,单条结果 function execute_dql2($sql){ $res=mysql_query($sql); return $res; } function conn_close(){ if(!empty($this->conn)){ mysql_close($this->conn); } } }