Menu

[r13]: / e9-core / db.class.php  Maximize  Restore  History

Download this file

124 lines (98 with data), 1.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
* MySQLi wrapper class
*/
class e_db
{
public $prefix;
private $db, $dblink, $query_count = 0;
function __construct( $arr )
{
$this->db = new mySQLi();
$this->db->init();
$this->db->real_connect( $arr['host'], $arr['user'], $arr['password'], $arr['db'], $arr['port'], $arr['sock'] );
$this->db->set_charset('utf8');
$this->prefix = $arr['prefix'];
}
function __destruct()
{
e_debug::log( "Page query count = " . $this->query_count );
$this->db->close();
}
/*
* Database query
*/
function res( $str, $debug = 0 )
{
if ( $debug )
{
e_debug::log( $str );
return;
}
$this->query_count++;
$ret = $this->db->real_query( $str );
if ( $ret )
return $ret;
else
{
e_debug::log( $this->db->error );
return false;
}
}
/*
* Fetch database row
*/
function row( $str, $debug = 0 )
{
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
return $result->fetch_array();
}
/*
* Fetch a column value from a database row
*/
function value( $str, $debug = 0 )
{
if ( !( $row = $this->row( $str, $debug ) ) )
return false;
return $row[0];
}
/*
* Fetch an array of single column values from database table rows
*/
function values( $str, $debug = 0 )
{
$arr = array();
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
while( $row = $result->fetch_row() )
$arr[] = $row[0];
return $arr;
}
/*
* Fetch array of rows
*/
function arr( $str, $debug = 0 )
{
$arr = array();
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
while( $row = $result->fetch_array() )
$arr[] = $row;
return $arr;
}
function last_id()
{
return mysqli_insert_id( $this->db );
}
};
?>