douyan3478 2015-12-01 01:05
浏览 30
已采纳

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

I am trying to do the trick of shortening my code not only for readability but also for the customisation of the project I am working on.

I have created a class which connects to the DataBase but am struggling with a function to use that will create a table with columns.

The class looks like this so far:

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.");
    }
}

As you can see the method of query has already been added. An example of how its run is:

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

Could anyone possible sent me some code to add the function to add the Inserting table? I have tried this:

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

Usage:

$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);
  • 写回答

1条回答 默认 最新

  • duanjing1276 2015-12-01 01:28
    关注

    I would recommend looking at MySQLi or PDO since you're using the deprecated function mysql which is vulnerable as it stands right now. I have updated your class (not tested) to get you started. This also fixes your original issue of not being able to create a table.

    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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体