<?php
/*
对数据库操作的助手类
*/
class SqlHelper{
private $conn;
private $host="127.0.0.1";
private $username="root";
private $password="123";
private $database="emp";
/**
* @return the $conn
*/
public function getConn() {
return $this->conn;
}
/**
* @return the $host
*/
public function getHost() {
return $this->host;
}
/**
* @return the $username
*/
public function getUsername() {
return $this->username;
}
/**
* @return the $password
*/
public function getPassword() {
return $this->password;
}
/**
* @return the $database
*/
public function getDatabase() {
return $this->database;
}
public function __construct(){
$this->conn=new mysqli($this->host,$this->username,$this->password,$this->database);
if(mysqli_connect_error()){
die("数据库连接错误".$this->conn->error);
}
}
//执行DQL语句
public function Execute_DQL($sql){
$arr=array();
$res=$this->conn->query($sql);
$i=0;
while($row=$res->fetch_assoc()){
$arr[$i++]=$row;
}
return $arr;
$res->free_result();
}
public function Execute_DQL_Array($sql){
$arr=array();
$res=$this->conn->query($sql);
$i=0;
while($row=$res->fetch_array()){
$arr[$i++]=$row;
}
return $arr;
$res->free_result();
}
//关闭连接
public function Close(){
$this->conn->close();
}
//执行DML语句
public function Execute_DML($sql){
$this->conn->query($sql);
echo $this->conn->affected_rows;
}
}
?>