在类中创建数据库表作为函数

Kyl*_*mas 5 php mysql database insert

我正在努力缩短我的代码,不仅是为了可读性,还为了我正在进行的项目的定制.

我创建了一个连接到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)
            OR die("There was a problem disconnecting from the database.");
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,已经添加了查询方法.它的运行方式的一个例子是:

$db = new DataBase('localhost',$user,$pass,$name);
$db->query('SELECT * FROM table WHERE id="0"');
Run Code Online (Sandbox Code Playgroud)

有没有人可以给我发一些代码来添加添加插入表的功能?我试过这个:

public function create_table($t_data) {
    $result = $t_data;
    if (!$result) die('Invalid query: ' . mysql_error());
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

用法:

$t_data = 'CREATE TABLE log_users(
     uid VARCHAR(1024) NOT NULL,
     username VARCHAR(33) NOT NULL,
     password VARCHAR(18) NOT NULL,
     admin VARCHAR(1) DEFAULT 0,
     key VARCHAR(18) NOT NULL,
     constant VARCHAR(1) DEFAULT 0)';

 $db->create_table($t_data);
Run Code Online (Sandbox Code Playgroud)

cam*_*ase 1

我建议您查看MySQLi或 ,PDO因为您正在使用已弃用的函数,mysql该函数目前很容易受到攻击。我已经更新了您的课程(未经测试)以帮助您入门。这也解决了您最初无法创建表的问题。

class DataBase {

    private $link;
    // May not need these, see updated __construct method
    private $host, $username, $password, $database;

    public function __construct($host, $username, $password, $database){
        // Unless you need them elsewhere, no reason to set $this->host, $this->username, etc...you can just access directly like below
        $this->link = new mysqli($host, $username, $password, $database);

        // Check connection (which also checks selection of database)
        if ($this->link->connect_error) {
            die("Connection failed: " . $this->link->connect_error);
        }
    }

    // You will need to research and update this to work with mysqli (right now it's ripe for SQL injection)!
    public function query($query) {
        $result = mysql_query($query);
        if (!$result) die('Invalid query: ' . mysql_error());
        return $result;
    }

    // This method will create a table based on the SQL you send it
    public function create_table($sql) {
        if ($this->link->query($sql) === TRUE) {
            return "Table created successfully";
        } else {
            return "Error creating table: " . $this->link->error;
        }
    }

    // Close connection
    public function __destruct() {
        $this->link->close();
    }
}
Run Code Online (Sandbox Code Playgroud)