形如 $this->$host = $host; 的
应写作形如 $this->host = $host; 这样的
否则不会正常工作

解决方案 »

  1.   

    你的调用方法并没有错,错的是类的成员属性是不能直接这样赋值的,$conn = new mysql_db()是不对的
    class DatebaseClass {
        public $conn;
        function __construct()
       { 
           $this->conn= new mysql_db(); 
        }
    }
      

  2.   

       $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;
      

  3.   

    谢谢大家帮我指出了一些错误 但是问题依旧 .. 我根据大家说的修改了以后,从新贴一遍完整的代码
    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")); 
      

  4.   


    你的 DatebaseClass 这个类里的构造函数放到了function里了,这样肯定不对吧
      

  5.   

    是不是在第十七行应该把mysql_fetch_object();写成mysql_fetch_assoc();啊??
      

  6.   

    第十七行应该把mysql_fetch_object();写成mysql_fetch_assoc();