<?php
class user_sql
{
private $testMode;
private $debugMode = false;
private $conn_id;
private $record;
private $db;
private $query_count;
public $dbPrefix;
public function __construct($DBARR)
{
$this->query_count = 0;
$this->testMode = $DBARR["testMode"];
$this->dbPrefix = $DBARR["dbPrefix"];
$this->db["host"] = $DBARR["host"];
$this->db["dbName"] = $DBARR["dbName"];
$this->db["user"] = $DBARR["user"];
$this->db["pass"] = $DBARR["pass"];
if (preg_match("/:/", $this->db['host'])) {
list($this->db['host'], $this->db['port']) = explode(":", $this->db['host']);
} else {
$this->db['port'] = 3306;
}
$this->connect();
}
public function connect()
{
$this->conn_id = mysqli_connect($this->db['host'] . ":" . $this->db['port'], $this->db['user'], $this->db['pass'], $this->db['dbName']);
if (!$this->conn_id) {
$this->sql_error("Connection Error");
}
return $this->conn_id;
}
public function query($query_string)
{
mysqli_query($this->conn_id, "SET NAMES UTF8");
mysqli_query($this->conn_id, "SET COLLATION_CONNECTION=UTF8");
$result = mysqli_query($this->conn_id, $query_string);
$this->query_count ++;
if (!$result) {
$this->sql_error("Query Error", $query_string);
}
return $result;
}
public function fetch_all($query_id)
{
return $query_id->fetch_all(MYSQLI_ASSOC);
}
public function fetch_array($query_id)
{
$this->record = mysqli_fetch_array($query_id);
return $this->record;
}
public function fetch_assoc($query_id)
{
$this->record = mysqli_fetch_assoc($query_id);
return $this->record;
}
public function fetch_column($query_id, $column_name)
{
return array_column($query_id->fetch_all(MYSQLI_ASSOC), $column_name);
}
public function num_rows($query_id)
{
return ($query_id) ? mysqli_num_rows($query_id) : 0;
}
public function num_fields($query_id)
{
return ($query_id) ? mysqli_num_fields($query_id) : 0;
}
public function escape_string($string)
{
return mysqli_real_escape_string($this->conn_id, $string);
}
public function free_result($query_id)
{
mysqli_free_result($query_id);
}
public function affected_rows()
{
return mysqli_affected_rows($this->conn_id);
}
public function last_inserted()
{
return mysqli_insert_id($this->conn_id);
}
public function close_db()
{
return $this->conn_id && mysqli_close($this->conn_id);
}
private function sql_error($message, $query_string = '')
{
if ($this->testMode) {
$description = mysqli_error($this->conn_id);
$number = mysqli_errno($this->conn_id);
$error = "MySQL Error : $message\n";
$error .= "Query String: $query_string\n";
$error .= "Error Number: $number $description\n";
$error .= "Date : " . date("D, F j, Y H:i:s") . "\n";
$error .= "IP : " . getenv("REMOTE_ADDR") . "\n";
$error .= "Browser : " . getenv("HTTP_USER_AGENT") . "\n";
$error .= "Referer : " . getenv("HTTP_REFERER") . "\n";
$error .= "PHP Version : " . PHP_VERSION . "\n";
$error .= "OS : " . PHP_OS . "\n";
$error .= "Server : " . getenv("SERVER_SOFTWARE") . "\n";
$error .= "Server Name : " . getenv("SERVER_NAME") . "\n";
$error .= "Script Name : " . getenv("SCRIPT_NAME") . "\n";
echo "<b style='font-family: Arial, sans-serif;'>$message</b><hr>";
} else {
$error = "MySQL Error...\n";
}
echo "<pre>$error</pre>";
if ($this->debugMode) {
echo "<hr><pre>";
debug_print_backtrace();
echo "</pre>";
}
exit();
}
}