封装有利于对工具的调用,代码的复用性和可维护性将大大提高
<?php
class Tool {
private $conn;
private $host="localhost";
private $user="root";
private $password="root";
private $db="test";
function Tool(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("连接数据库失败".mysql_error());
}
mysql_select_db($this->db,$this->conn);
mysql_query("set names utf8");
}
// 完成选择数据表 dql
public function execute_dql($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_error());
return $res;
}
//完成 update,delete ,insert dml 等操作
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
//echo "添加的id=".mysql_insert_id($this->conn);
if(!$b){
return 0;//失败
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示成功
}else{
return 2;//表示没有行数影响.
}
}
}
}
?>