错误“在不在对象上下文中时使用$ this”与类

时间:2013-09-19 04:15:36

标签: php

我有一个这样的课程:

<?php
    class DataConnection {

        private $hostName = "localhost";
        private $username = "root";
        private $password = "";
        private $dbName = "test";
        private $link = "";

        public function __construct() {

        }

        private function connect() {

            $this->link = mysql_connect($this->hostName, $this->username, $this->password)
                    or die ("Could not connect to the database");

            mysql_select_db($this->dbName, $this->link) or die("Could not select database");

        }

        // lay kieu du lieu cua record trong table
        public function getDataTypeTable($tableName) {

            $this->connect();// ERROR HERE

            $query = "SELECT * FROM {$tableName}";
            $result = mysql_query($query);

            $fields = mysql_num_fields($result);
            $rows   = mysql_num_rows($result);
            $table  = mysql_field_table($result, 0);
            echo "Your '" . $table . "' table has " . $fields . " fields and " . $rows . " record(s)\n";
            echo "The table has the following fields:\n";
            for ($i=0; $i < $fields; $i++) {
                $type  = mysql_field_type($result, $i);
                $name  = mysql_field_name($result, $i);
                $len   = mysql_field_len($result, $i);
                $flags = mysql_field_flags($result, $i);
                echo $type . " " . $name . " " . $len . " " . $flags . "\n";
            }

            //var_dump($results);

            //mysql_free_result($results);

        }

        public function __destruct() {
            mysql_close($this->link);
        }

    }
?>

我收到错误“致命错误:在第44行的C:\ xampp \ htdocs \ demo \ test \ DataConnection.php中没有对象上下文时使用$ this”,这意味着connect函数出错了。我该如何解决?谢谢观看!

0 个答案:

没有答案