怎么调用另一个文件中的类 形如 $this->$host = $host; 的应写作形如 $this->host = $host; 这样的否则不会正常工作 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的调用方法并没有错,错的是类的成员属性是不能直接这样赋值的,$conn = new mysql_db()是不对的class DatebaseClass { public $conn; function __construct() { $this->conn= new mysql_db(); }} $this->$host = $host; $this->$user = $user; $this->$pass = $pass; $this->$charset = $charset; $this->$name = $name; $this->$tablepre = $tablepre;改成 $this->host = $host; $this->user = $user; $this->pass = $pass; $this->charset = $charset; $this->name = $name; $this->tablepre = $tablepre; 谢谢大家帮我指出了一些错误 但是问题依旧 .. 我根据大家说的修改了以后,从新贴一遍完整的代码mysql_db.php(连接数据库) require('mysql_db_config.php'); class mysql_db { public $host; //主机地址 public $user; //用户名 public $pass; //用户密码 public $charset; //字符集 public $name; //数据库名 public $tablepre; //表前缀 public $conn; //数据库连接变量 function __construct($host=DB_HOST, $user=DB_USER, $pass=DB_PASS, $charset=DB_CHARSET, $name=DB_NAME, $tablepre=TABLEPRE) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->charset = $charset; $this->name = $name; $this->tablepre = $tablepre; //echo "主机1:" . $config['db']['dbhost'] . "<br>"; 这里输出正常 //echo "主机2:" . $this->$config['db']['dbhost']; } /* * 打开数据库连接 */ public function getConn() { $this->conn = mysql_connect($this->host, $this->user, $this->pass) or die('could not connect:' . mysql_error()); //连接数据库表 mysql_select_db($this->name); //数据库编码 mysql_query("SET CHARACTER SET ". $this->charset); return $this->conn; } /* * 关闭数据库连接 */ public function close() { mysql_close($this->conn); } } $db = new mysql_db(); echo $db->getConn(); //这里仍然输出Resource id #4DatebaseClass.php require('mysql_db.php'); class DatebaseClass { public $conn; /* * 查询数据 */ public function getObjListBySql($sql) { function __construct() { $this->conn = new mysql_db(); } $link = $this->conn->getConn(); $result = mysql_query($sql, $link); $objList = array(); while($obj = mysql_fetch_object($result)) { if ($obj) { $objList[] = $obj; } } $this->conn->close(); return $objList; } } $dc = new DatebaseClass(); //下面输出 Resource id #5 print_r($dc->getObjListBySql("SELECT * FROM gc_blog_srecords")); 你的 DatebaseClass 这个类里的构造函数放到了function里了,这样肯定不对吧 是不是在第十七行应该把mysql_fetch_object();写成mysql_fetch_assoc();啊?? 第十七行应该把mysql_fetch_object();写成mysql_fetch_assoc(); 多表联合查询求助 cookie在不同目录下取不到值 php中有类似于asp.net中的treeview控件吗? 连不上mysql php url 传值问题 好东西,可以下载收藏哦 因公司需要从.NET转到PHP谁能给推荐几本好书? 奇怪的问题,来看看 用adodb这个来用access是否一定要在win系统下 请教:数据库日期查询。 PHP url地址加密疑问 紧急求组,数据库中存储的arra字段如何输出
class DatebaseClass {
public $conn;
function __construct()
{
$this->conn= new mysql_db();
}
}
$this->$user = $user;
$this->$pass = $pass;
$this->$charset = $charset;
$this->$name = $name;
$this->$tablepre = $tablepre;
改成
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->charset = $charset;
$this->name = $name;
$this->tablepre = $tablepre;
mysql_db.php(连接数据库) require('mysql_db_config.php');
class mysql_db {
public $host; //主机地址
public $user; //用户名
public $pass; //用户密码
public $charset; //字符集
public $name; //数据库名
public $tablepre; //表前缀
public $conn; //数据库连接变量
function __construct($host=DB_HOST, $user=DB_USER, $pass=DB_PASS, $charset=DB_CHARSET, $name=DB_NAME, $tablepre=TABLEPRE) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->charset = $charset;
$this->name = $name;
$this->tablepre = $tablepre;
//echo "主机1:" . $config['db']['dbhost'] . "<br>"; 这里输出正常
//echo "主机2:" . $this->$config['db']['dbhost'];
}
/*
* 打开数据库连接
*/
public function getConn() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass) or die('could not connect:' . mysql_error());
//连接数据库表
mysql_select_db($this->name);
//数据库编码
mysql_query("SET CHARACTER SET ". $this->charset);
return $this->conn;
}
/*
* 关闭数据库连接
*/
public function close() {
mysql_close($this->conn);
}
}
$db = new mysql_db();
echo $db->getConn(); //这里仍然输出Resource id #4DatebaseClass.php require('mysql_db.php');
class DatebaseClass {
public $conn;
/*
* 查询数据
*/
public function getObjListBySql($sql) {
function __construct() {
$this->conn = new mysql_db();
}
$link = $this->conn->getConn();
$result = mysql_query($sql, $link);
$objList = array();
while($obj = mysql_fetch_object($result)) {
if ($obj) {
$objList[] = $obj;
}
}
$this->conn->close();
return $objList;
}
}
$dc = new DatebaseClass();
//下面输出 Resource id #5
print_r($dc->getObjListBySql("SELECT * FROM gc_blog_srecords"));
你的 DatebaseClass 这个类里的构造函数放到了function里了,这样肯定不对吧