I am trying to do the trick of shortening my code, not only for readability, but also for setting up the project I'm working on.
I created a class that connects to a DataBase, but I'm struggling with a function to use that will create a table with columns.
The class looks like this:
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 request method has already been added. An example of how it is executed:
$db = new DataBase('localhost',$user,$pass,$name); $db->query('SELECT * FROM table WHERE id="0"');
Can someone send me a code to add a function to add an insert table? I tried this:
public function create_table($t_data) { $result = $t_data; if (!$result) die('Invalid query: ' . mysql_error()); return $result; }
Using:
$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);