我正在努力缩短我的代码,不仅是为了可读性,还为了我正在进行的项目的定制.
我创建了一个连接到DataBase的类,但我正在努力使用一个函数来创建一个包含列的表.
到目前为止,这个类看起来像这样:
class DataBase {
private $link;
private $host, $username, $password, $database;
public function __construct($host, $username, $password, $database){
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->link = mysql_connect($this->host, $this->username, $this->password)
OR die("There was a problem connecting to the database.");
mysql_select_db($this->database, $this->link)
OR die("There was a problem selecting the database.");
return true;
}
public function query($query) {
$result = mysql_query($query);
if (!$result) die('Invalid query: ' . mysql_error());
return $result;
}
public function __destruct() {
mysql_close($this->link) …Run Code Online (Sandbox Code Playgroud)