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); }}
$conn = new Conn();$sql = "select * from qj_content";$contentList = $conn->getDataArrays($sql,MYSQL_ASSOC);$conn->closeConn();
那块用红色标注的被弄成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);?>
query 方法改成这样
public function query($sql){ $rs = mysql_query($sql,$this->conn) or die(mysql_error()."<br/>SQL执行出错:$sql"); return $rs; }