私はこのようなクラスを持っています:
<?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);
}
}
?>
「致命的なエラー: Using $this when not in object context in C:\xampp\htdocs\demo\test\DataConnection.php on line 44」というエラーが表示されます。これは、connect 関数に問題があることを意味します。どうすれば修正できますか?見てくれてありがとう!