Creating a DataBase Table inside a Class as a Function

Kyle Thomas

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);
camelCase

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();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a function inside a function inside a class

From Dev

Creating and calling a custom function inside an angular class

From Dev

Creating a Global Instance of a Class Inside of a Function

From Dev

Creating Unittest class inside function not working

From Java

Java: Creating Mysql Database and Table in the same class

From Java

Creating entity class using database existing table?

From Dev

New Grails domain class is not creating a table in the database

From Dev

Is it Logical Creating Separate Java Class for Database Table?

From Dev

Ruby on Rails - Creating a database table within different class

From Dev

SQLIte not creating database table

From Dev

database table is not creating

From Dev

creating a table in database

From Dev

Conditionally creating members inside a class

From Dev

Creating a class object inside a loop

From Dev

Creating buttons inside a class with Tkinter

From Dev

Creating a function table

From Dev

Creating a Table of Function Outputs

From Java

Creating global variable or creating instance inside of function

From Dev

Creating function inside Controller AngularJS

From Dev

creating a javascript function inside textarea

From Dev

SQLite DatabaseHelper class is not creating database

From Dev

i need Database table creating

From Dev

Error creating table: No database selected

From Dev

creating table in mysql database with codeigniter

From Dev

Creating class object inside same class in Python

From Dev

Problems while creating a class inside of a class

From Dev

Async function inside a class

From Dev

Function inside class not executed

From Dev

Function inside a class not triggering

Related Related

HotTag

Archive