Skip to content

Instantly share code, notes, and snippets.

@RoverWire
Created March 27, 2012 17:44
Show Gist options
  • Save RoverWire/2218296 to your computer and use it in GitHub Desktop.
Save RoverWire/2218296 to your computer and use it in GitHub Desktop.
Tiny MySQL connection class
<?php
/**
*
*/
class Connection
{
private $result;
private $db = array(
'write' => array(
'host' => 'localhost',
'user' => '',
'pass' => '',
'database' => 'voip'
),
'read' => array(
'host' => 'localhost',
'user' => '',
'pass' => '',
'database' => 'voip'
)
);
protected $show_querys = FALSE;
protected $show_error = FALSE;
protected $link = array();
protected $insert_id;
protected $affected_rows;
protected $total_rows;
protected $status = array();
public function __construct()
{
foreach ($this->db as $key => $value) {
$this->connect($key);
}
}
private function check_active($group)
{
if(! array_key_exists($group, $this->db)){
$message = "Connection group $group does not exists";
if($this->show_error){
die($message);
}else{
$this->status[$group] = $message;
}
return FALSE;
}else{
return TRUE;
}
}
private function error($msg, $group)
{
if($this->show_error){
die($msg);
}else{
$this->status[$group][] = $msg;
}
}
protected function connect($group)
{
$this->check_active($group);
$this->link[$group] = @mysql_connect($this->db[$group]['host'], $this->db[$group]['user'], $this->db[$group]['pass']);
if(! $this->link[$group]){
$message = 'Could not connect: ' . mysql_error();
$this->error($message, $group);
}
if(! @mysql_select_db($this->db[$group]['database'], $this->link[$group])){
$message = 'Could not select database '.$this->db[$group]['database'];
$this->error($message, $group);
$this->close($group);
}
}
public function query($str_sql, $group = 'read')
{
if(! empty($str_sql)){
$this->check_active($group);
$this->result = mysql_query($str_sql, $this->link[$group]);
if(! $this->result){
$message = "Query error: ".mysql_errno();
$this->error($message, $group);
if($this->show_querys){
echo $str_sql;
}
}
$this->total_rows = mysql_num_rows($this->result);
return array($this->result, $this->total_rows, FALSE);
}else{
return FALSE;
}
}
public function update($str_sql, $group = 'write')
{
if(! empty($str_sql)){
$this->check_active($group);
$this->result = mysql_query($str_sql, $this->link[$group]);
if(! $this->result){
$message = "Query error: ".mysql_errno();
$this->error($message, $group);
if($this->show_querys){
echo $str_sql;
}
}
$this->affected_rows = mysql_affected_rows($this->link[$group]);
$this->insert_id = mysql_insert_id($this->link[$group]);
return array(FALSE, $this->affected_rows, $this->insert_id);
}else{
return FALSE;
}
}
public function close($group = '')
{
if(! empty($group) && $this->check_active($group)){
@mysql_close($this->link[$group]);
}else{
foreach ($this->db as $key => $value) {
@mysql_close($this->link[$key]);
}
}
}
public function set_show_errors($value = FALSE)
{
$this->show_error = ($value == TRUE) ? TRUE:FALSE;
}
public function set_show_querys($value = FALSE)
{
$this->show_querys = ($value == TRUE) ? TRUE:FALSE;
}
public function check_status()
{
foreach ($this->status as $log) {
foreach ($log as $txt) {
echo $txt."<br>";
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment