无语的出错,mysql_fetch_array()

WBOY
Release: 2016-06-23 14:19:24
Original
1091 people have browsed it

MySQL PHP 数据库连接 查询

代码如下,求帮忙看一下吧!
<?phpclass 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);	}}
Copy after login



调用如下:
$conn = new Conn();$sql = "select * from qj_content";$contentList = $conn->getDataArrays($sql,MYSQL_ASSOC);$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行代码在上面用红色标注了


回复讨论(解决方案)

那块用红色标注的被弄成php代码了,我晕
是/**
* 返回多条记录
*/
public function getDataArrays($sql,$type = MYSQL_BOTH){
$result = $this->query($sql);
$refArr = array();
while ($row = mysql_fetch_array($result,$type)){
$refArr[] = $row;
}
return $refArr;
}

怎么都没有人来?
几次提问 不论简单还是困难都没有人来帮帮解答一下,
CSDN什么时候变得这么冷清了?

估计sql 出问题了
在while 上 echo mysql_error(); 看看

LZ看看W3C上面的例子吧



语法

mysql_fetch_array(data,array_type)

参数 描述
data 可选。规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果。
array_type

可选。规定返回哪种结果。可能的值:

MYSQL_ASSOC - 关联数组
MYSQL_NUM - 数字数组
MYSQL_BOTH - 默认。同时产生关联和数字数组




<?php$con = mysql_connect("localhost", "hello", "321");if (!$con)  {  die('Could not connect: ' . mysql_error());  }$db_selected = mysql_select_db("test_db",$con);$sql = "SELECT * from Person WHERE Lastname='Adams'";$result = mysql_query($sql,$con);print_r(mysql_fetch_array($result));mysql_close($con);?>
Copy after login

query 方法改成这样

    public function query($sql){        $rs = mysql_query($sql,$this->conn) or die(mysql_error()."<br/>SQL执行出错:$sql");        return $rs;    }
Copy after login

因为 mysql_query($sql,$this->conn)  or die(mysql_error()."
SQL执行出错:$sql")
是一个逻辑表达式,直接返回的话只能是逻辑值。
所以要写作
$rs = mysql_query($sql,$this->conn) or die(mysql_error()."
SQL执行出错:$sql");
因为 = 的优先级高于 or 所以 $rs = mysql_query($sql,$this->conn) 被先执行
算式变为
$rs or die(mysql_error()."
SQL执行出错:$sql");
当然还是逻辑表达式啦,但其结果被抛弃了
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