时间:2021-07-01 10:21:17 帮助过:4人阅读
class Conn{
private $dbhost = ""; //服务器
private $dbuser = ""; //mysql帐号
private $dbpsw = ""; //mysql密码
private $dbchar = ""; //数据库编码类型
private $dbname = ""; //数据库名称
private $tablepre = ""; //表前缀
private $conn = NULL; //连接对象
/**
* 数据库链接初始化
*/
public function __construct(){
$this->getConn();
}
public function getConn(){
$db = require_once 'config/config.php';
$this->dbhost = $db['dbhost'];
$this->dbuser = $db['dbuser'];
$this->dbpsw = $db['dbpsw'];
$this->dbchar = $db['dbchar'];
$this->dbname = $db['dbname'];
$this->tablepre = $db['tablepre'];
$this->conn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpsw) or die(mysql_error()."
Mysql连接失败!");
mysql_select_db($this->dbname,$this->conn) or die(mysql_error()."
数据库访问出错");
mysql_query("set names ".$this->dbchar,$this->conn);
}
/**
* 执行sql
*/
public function query($sql){
return mysql_query($sql,$this->conn) or die(mysql_error()."
SQL执行出错:$sql");
}
/**
* 返回多条记录
*/
public function getDataArrays($sql,$type = MYSQL_BOTH){
$result = $this->query($sql);
$refArr = array();
while ($row = mysql_fetch_array($result,$type)){
$refArr[] = $row;
}
return $refArr;
}
/**
* 关闭数据库链接
*/
public function closeConn(){
mysql_close($this->conn);
}
}
$conn = new Conn();
$sql = "select * from qj_content";
$contentList = $conn->getDataArrays($sql,MYSQL_ASSOC);
$conn->closeConn();