Last active
October 17, 2017 16:40
-
-
Save mitchellurgero/8f86c96c6773cf56672a4153a4da4e44 to your computer and use it in GitHub Desktop.
A simple MySQLi class to make using MySQLi in PHP a little bit easier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************* | |
* Bitcoin Cash (BCH/BCC) | |
* 1DaXBwnUgWcPuNvEzei9KMkodjxF6rSSw6 | |
* | |
*/ | |
<?php | |
class db { | |
/** | |
* string containing the user's query string | |
*/ | |
private $query; | |
private $query_results; | |
function __construct($username, $password, $connect_now = false, $database = 'default', $host = 'localhost', $port = '3306') { | |
$this->username = $username; | |
$this->password = $password; | |
$this->host = $host; | |
$this->port = $port; | |
$this->name = $database; | |
$this->query_results = NULL; | |
if($connect_now) | |
if(!$this->connect()) die('ERROR: Unable to connect to database'); | |
$this->parent = parent::$object; | |
parent::$children[get_class($this)] = $this; | |
} | |
public function connect() { | |
$this->connection = new mysqli($this->host, $this->username, $this->password, $this->name); | |
if(!$this->hasValidDbConnection()) return false; | |
return true; | |
} | |
public function run_query($query, $close_connection = false, $array_type = false) { | |
if(!$this->hasValidDbConnection()) return false; | |
$return = $this->query_results = $this->connection->query($query); | |
if($array_type !== false) { | |
$fetch = 'fetch_'.$array_type; | |
$return = []; | |
while($row = $this->query_results->$fetch()) | |
$return[] = $row; | |
} | |
if($close_connection) $this->close(); | |
return $return; | |
} | |
public function run_multi_query($query, $close_connection = false) { | |
if(!$this->hasValidDbConnection()) return false; | |
$return = $this->connection->multi_query($query); | |
if($close_connection) $this->close(); | |
return $return; | |
} | |
public function close() { | |
// This line caused db query results to clear before returning to calling function | |
// $this->query_results->free(); | |
$this->query_results = NULL; | |
$this->connection->kill($this->connection->thread_id); | |
$this->connection->close(); | |
} | |
private function hasValidDbConnection() { | |
return (bool) $this->connection; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment