Skip to content

Instantly share code, notes, and snippets.

@MisterFixx
Created August 17, 2019 18:37
Show Gist options
  • Save MisterFixx/12ba9058dc03c41d018f8d924c5d4872 to your computer and use it in GitHub Desktop.
Save MisterFixx/12ba9058dc03c41d018f8d924c5d4872 to your computer and use it in GitHub Desktop.
BetterMySQLi.php
<?php
/**
* BetterMySQLi.php
*
* PHP class that allows the usage of some MySQLi functions inline to save space on short queries
* @author Mister_Fix <me@misterfix.io>
* @package BetterMySQLi
* @version v 1.0
* @license MIT License
*/
class BetterMySQLi{
private $conn = null;
private $stmt = null;
private $result = null;
/**
* Initiates a new MySQLi connection. you can use this without porting the rest of your code to this class
* because the methods here use the same name as their native counterpart.
*
* @param String $host - The MySQL server's address.
* @param String $user - Your MySQL username.
* @param String $password - Your MySQL password.
* @param String $database - Your MySQL database.
*
* @return BetterMySQLi $this - the BetterMySQLi object.
*/
public function __construct($host, $user, $password, $database){
$this->conn = new mysqli($host, $user, $password, $database);
return $this;
}
/**
* Prepare a MySQLi query
*
* @see https://www.php.net/manual/en/mysqli.prepare.php
* @throws RuntimeException - if no connection was made prior to attempting to prepare a query.
*/
public function prepare($query){
if(!is_null($this->conn)){
$this->stmt = $this->conn->prepare($query);
return $this;
}
else{
throw new RuntimeException('The database connection is null.');
}
}
/**
* Bind parameters to a query
*
* @see https://www.php.net/manual/en/mysqli-stmt.bind-param.php
* @return BetterMySQLi $this - the BetterMySQLi object.
* @throws RuntimeException - if no prepared statement was made prior to attempting to bind params.
*/
public function bind_param($types, &...$params){
if(!is_null($this->stmt)){
$this->stmt->bind_param($types, ...$params);
return $this;
}
else{
throw new RuntimeException('The prepared statement is null.');
}
}
/**
* Execute a query
*
* @see https://www.php.net/manual/en/mysqli-stmt.execute.php
* @return BetterMySQLi $this - the BetterMySQLi object.
* @throws RuntimeException - if no prepared statement was made prior to attempting to execute.
*/
public function execute(){
if(!is_null($this->stmt)){
$this->stmt->execute();
$this->result = $this->stmt->get_result();
return $this;
}
else{
throw new RuntimeException('The prepared statement is null.');
}
}
/**
* Get query results
*
* @see https://www.php.net/manual/en/mysqli-stmt.get-result.php
* @return BetterMySQLi $this - the BetterMySQLi object.
* @throws RuntimeException - if no prepared statement was made prior to attempting to execute.
*/
public function get_result(){
if(!is_null($this->result)){
return $this->result;
}
else{
throw new RuntimeException('There is no MySQLi result stored.');
}
}
/**
* was found
* This method could be used to check whether at least 1 row was found matching your query,
* useful for quickly looking up for the existance of something in the database.
*
* @return Boolean false in num_rows is 0, true otherwise.
* @throws RuntimeException - if no result was stored before an attempt to check result set size.
*/
public function found(){
if(!is_null($this->result)){
return $this->result->num_rows != 0;
}
else{
throw new RuntimeException('There is no MySQLi result stored.');
}
}
/**
* Get results as associative array
*
* @see https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
* @return Array result set as associatve array.
* @throws RuntimeException - if no result was stored before an attempt to get result set.
*/
public function fetch_assoc(){
if(!is_null($this->result)){
return $this->result->fetch_assoc();
}
else{
throw new RuntimeException('There is no MySQLi result stored.');
}
}
/**
* Close MySQLi connection
*
* @see https://www.php.net/manual/en/mysqli.close.php
* @return BetterMySQLi $this - the BetterMySQLi object.
*/
public function close(){
$this->conn->close();
$this->conn = null;
$this->stmt = null;
$this->result = null;
return $this;
}
/**
* Open a new MySQLi connection
*
* @param String $host - The MySQL server's address.
* @param String $user - Your MySQL username.
* @param String $password - Your MySQL password.
* @param String $database - Your MySQL database.
* @throws RuntimeException - if previous connection was not closed prior to opening a new one.
*
* @return BetterMySQLi $this - the BetterMySQLi object.
*/
public function open($host, $user, $password, $database){
if(is_null($this->conn)){
$this->conn = new mysqli($host, $user, $password, $database);
return $this;
}
else{
throw new RuntimeException('Cannot open new connection because the current one is still open! close the current connection before opening a new one.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment