代码如下,求帮忙看一下吧!
<?php
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()."<br/>Mysql连接失败!");
mysql_select_db($this->dbname,$this->conn) or die(mysql_error()."<br/>数据库访问出错");
mysql_query("set names ".$this->dbchar,$this->conn);
}
/**
* 执行sql
*/
public function query($sql){
return mysql_query($sql,$this->conn) or die(mysql_error()."<br/>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();
执行结果如下:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\qjcentury\conn.php on line 45
第45行代码在上面用红色标注了
MySQL
PHP
数据库连接
查询