0

我有这样的课:

<?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”,这意味着连接函数有问题。我该如何解决?感谢收看!

4

0 回答 0