Home > Backend Development > PHP Tutorial > 无语的失误,mysql_fetch_array()

无语的失误,mysql_fetch_array()

WBOY
Release: 2016-06-13 12:39:24
Original
710 people have browsed it

无语的出错,mysql_fetch_array()
代码如下,求帮忙看一下吧!

<?php<br />
class Conn{<br />
	private $dbhost = "";			//服务器<br />
	private $dbuser = "";			//mysql帐号<br />
	private $dbpsw = "";			//mysql密码<br />
	private $dbchar = "";			//数据库编码类型<br />
	private $dbname = "";			//数据库名称<br />
	private $tablepre = "";			//表前缀<br />
	private $conn = NULL;			//连接对象 <br />
	<br />
	/**<br />
	 * 数据库链接初始化<br />
	 */<br />
	public function __construct(){<br />
		$this->getConn();<br />
	}<br />
	<br />
	public function getConn(){<br />
		$db = require_once 'config/config.php';<br />
		$this->dbhost = $db['dbhost'];<br />
		$this->dbuser = $db['dbuser'];<br />
		$this->dbpsw = $db['dbpsw'];<br />
		$this->dbchar = $db['dbchar'];<br />
		$this->dbname = $db['dbname'];<br />
		$this->tablepre = $db['tablepre'];<br />
		$this->conn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpsw) or die(mysql_error()."<br/>Mysql连接失败!");<br />
		mysql_select_db($this->dbname,$this->conn) or die(mysql_error()."<br/>数据库访问出错");<br />
		mysql_query("set names ".$this->dbchar,$this->conn);<br />
	}<br />
	<br />
	/**<br />
	 * 执行sql<br />
	 */<br />
	public function query($sql){<br />
		return mysql_query($sql,$this->conn) or die(mysql_error()."<br/>SQL执行出错:$sql");<br />
	}<br />
	<br />
	/**<br />
	 * 返回多条记录<br />
	 */<br />
	public function getDataArrays($sql,$type = MYSQL_BOTH){<br />
		$result = $this->query($sql);<br />
		$refArr = array();<br />
		<span style="color: #FF0000;">while ($row = mysql_fetch_array($result,$type)){</span><br />
			$refArr[] = $row;<br />
		}<br />
		return $refArr;<br />
	}<br />
<br />
	<br />
	/**<br />
	 * 关闭数据库链接<br />
	 */<br />
	public function closeConn(){<br />
		mysql_close($this->conn);<br />
	}<br />
}
Copy after login



调用如下:
$conn = new Conn();<br />
$sql = "select * from qj_content";<br />
$contentList = $conn->getDataArrays($sql,MYSQL_ASSOC);<br />
$conn->closeConn();
Copy after login


执行结果如下:
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 数据库连接 查询
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template