PHP DB-Klasse
28.05.2009
15:38:45 Uhr5079 Aufrufe
3 Antworten
15:38:45 Uhr
3 Antworten
Hilfreich +2
Da ich mal wieder eine Anfrage für eine DB-Klasse per PN bekam, veröffentliche ich diese einfach mal.
Hier eine PHP DB-Klasse die UTF-8 Verbindungen aufbaut. Ursprünglich kommt sie aus einem "MYSQL-Buch" von Michael Kofler. Ich habe sie hier und da ein wenig angepasst. Verbesserungsvorschläge, Erweiterungen etc. sind natürlich willkommen. Die Klasse benutzt PHP5 und mysqli.
Viel Spaß damit
Gruß
Frank
01.
<? 02.
/** 03.
* DB Class 04.
* 05.
* THIS DB Class USE UTF-8! 06.
* FOR ISO CONNECTION COMMENT THIS ROW: $utf = $this->queryExecute("SET NAMES 'utf8'"); OUT! 07.
* 08.
* DB Connect : 09.
* $db = new MyDb("localhost","dbuser","dbpass","dbname"); 10.
* 11.
* Examples: 12.
* 13.
* only one result: 14.
* ---------------- 15.
* if (($n = $db->querySingleItem("SELECT COUNT(*) FROM titles"))!=-1) { 16.
* printf("<p>Anzahl der Titel: %d</p>",$nr); 17.
* } 18.
* 19.
* more results: 20.
* ------------- 21.
* if ($result=$db->queryObjectArray("SELECT * FROM titles")) { 22.
* foreach ($result as $row) { 23.
* printf("<br />TitleID:%d Title=%s Subtitle=%s\",$row->titleID,$row->title,$row->subtitle); 24.
* } 25.
* } 26.
* 27.
* count a select with affectedRows: 28.
* --------------- 29.
* if ($nr=$db->queryExecute("SELECT * FROM titles")) { 30.
* printf("<p>Anzahl der Titel: %d</p>",$nr); 31.
* } 32.
* 33.
* execute a sql without result (only true/false returns) 34.
* ------------------------------------------------------- 35.
* if ($result=$db->execute("UPDATE titles SET titel='my new Titel'")) { 36.
* echo "successful"; 37.
* } 38.
* 39.
* if you like to see the short statistic use: 40.
* $db->showStatistics(); 41.
* in your programm. 42.
* 43.
*/ 44.
45.
class MyDb { 46.
protected $mysqli; 47.
protected $showerror = true; // set FALSE if you don't want to see error messages 48.
protected $showsql = false; // set TRUE if you want to see all SQL queries for debugging purposes 49.
protected $sqlcounter = 0; // counter for SQL commands 50.
protected $rowcounter = 0; // counter for returned SELECT rows 51.
protected $dbtime = 0; // counter for time needed to execute queries 52.
protected $starttime; 53.
54.
// constructor 55.
function __construct($dbhost, $dbuser, $dbpass, $dbname) { 56.
$this->mysqli = @new mysqli($dbhost, $dbuser, $dbpass, $dbname); 57.
// test, connect ok? 58.
if(mysqli_connect_errno()) { 59.
$this->printerror("Sorry, no connection! (" . mysqli_connect_error() . ")"); 60.
// you might add output for HTML code to close the page 61.
// here (</body></html> etc.) 62.
$this->mysqli = FALSE; 63.
exit(); 64.
} 65.
// SET DB INTO UTF8 MODE 66.
$utf = $this->queryExecute("SET NAMES 'utf8'"); 67.
$this->starttime = $this->microtime_float(); 68.
} 69.
70.
// destructor 71.
function __destruct() { 72.
$this->close(); 73.
} 74.
75.
// explicit close 76.
function close() { 77.
if($this->mysqli) 78.
$this->mysqli->close(); 79.
$this->mysqli = FALSE; 80.
} 81.
82.
function getMysqli() { 83.
return $this->mysqli; 84.
} 85.
86.
// execute SELECT query, return array 87.
function queryObjectArray($sql) { 88.
$this->sqlcounter++; 89.
$this->printsql($sql); 90.
$time1 = $this->microtime_float(); 91.
$result = $this->mysqli->query($sql); 92.
$time2 = $this->microtime_float(); 93.
$this->dbtime += ($time2 - $time1); 94.
if($result) { 95.
if($result->num_rows) { 96.
while($row = $result->fetch_object()) 97.
$result_array[] = $row; 98.
$this->rowcounter += sizeof($result_array); 99.
return $result_array; } 100.
else 101.
return FALSE; 102.
} else { 103.
$this->printerror($this->mysqli->error); 104.
return FALSE; 105.
} 106.
} 107.
108.
// execute SELECT query, return array 109.
function queryArray($sql) { 110.
$this->sqlcounter++; 111.
$this->printsql($sql); 112.
$time1 = $this->microtime_float(); 113.
$result = $this->mysqli->query($sql); 114.
$time2 = $this->microtime_float(); 115.
$this->dbtime += ($time2 - $time1); 116.
if($result) { 117.
if($result->num_rows) { 118.
while($row = $result->fetch_array()) 119.
$result_array[] = $row; 120.
$this->rowcounter += sizeof($result_array); 121.
return $result_array; 122.
}else { 123.
return FALSE; 124.
} 125.
} else { 126.
$this->printerror($this->mysqli->error); 127.
return FALSE; 128.
} 129.
} 130.
131.
132.
// execute a SELECT query which returns only a single 133.
// item (i.e. SELECT COUNT(*) FROM table); return 134.
// this item 135.
// beware: this method return -1 for errors (not 0)! 136.
function querySingleItem($sql) { 137.
$this->sqlcounter++; 138.
$this->printsql($sql); 139.
$time1 = $this->microtime_float(); 140.
$result = $this->mysqli->query($sql); 141.
$time2 = $this->microtime_float(); 142.
$this->dbtime += ($time2 - $time1); 143.
if($result) { 144.
if ($row=$result->fetch_array()) { 145.
$result->close(); 146.
$this->rowcounter++; 147.
return $row[0]; 148.
} else { 149.
// query returned no data 150.
return -1; 151.
} 152.
} else { 153.
$this->printerror($this->mysqli->error); 154.
return -1; 155.
} 156.
} 157.
158.
// execute a SQL command with the number of affected rows as results (no query) 159.
function queryExecute($sql) { 160.
$this->sqlcounter++; 161.
$this->printsql($sql); 162.
$time1 = $this->microtime_float(); 163.
$result = $this->mysqli->real_query($sql); 164.
$time2 = $this->microtime_float(); 165.
$this->dbtime += ($time2 - $time1); 166.
if($result) { 167.
// return TRUE; 168.
return $this->mysqli->affected_rows; 169.
} else { 170.
$this->printerror($this->mysqli->error); 171.
return FALSE; 172.
} 173.
} 174.
175.
// execute a SQL command without results, only true/false (no query) 176.
function execute($sql) { 177.
$this->sqlcounter++; 178.
$this->printsql($sql); 179.
$time1 = $this->microtime_float(); 180.
$result = $this->mysqli->real_query($sql); 181.
$time2 = $this->microtime_float(); 182.
$this->dbtime += ($time2 - $time1); 183.
if($result) { 184.
return TRUE; 185.
} else { 186.
$this->printerror($this->mysqli->error); 187.
return FALSE; 188.
} 189.
} 190.
191.
192.
// get insert_id after an INSERT command 193.
function insertId() { 194.
return $this->mysqli->insert_id; 195.
} 196.
197.
// get the number of affected rows after query command 198.
function affectedRows() { 199.
return $this->mysqli->affected_rows; 200.
} 201.
202.
// get mysql system status 203.
function mysqlStats() { 204.
return $this->mysqli->stat(); 205.
} 206.
207.
// insert \ before ', " etc. 208.
function escape($txt) { 209.
return trim($this->mysqli->escape_string($txt)); 210.
} 211.
212.
// return 'NULL' or '<quoted string>' 213.
function sql_string($txt) { 214.
if(!$txt || trim($txt)=="") 215.
return 'NULL'; 216.
else 217.
return "'" . $this->escape(trim($txt)) . "'"; 218.
} 219.
220.
function error() { 221.
return $this->mysqli->error; 222.
} 223.
224.
private function printsql($sql) { 225.
if($this->showsql) 226.
printf("<p><font color=\"#0000ff\">%s</font></p>\n", 227.
htmlspecialchars($sql)); 228.
} 229.
230.
private function printerror($txt) { 231.
if($this->showerror) 232.
printf("<p><font color=\"#ff0000\">%s</font></p>\n", 233.
htmlspecialchars($txt)); 234.
} 235.
236.
function showStatistics() { 237.
$totalTime = $this->microtime_float() - $this->starttime; 238.
printf("<p><font color=\"#0000ff\">SQL commands: %d\n", 239.
$this->sqlcounter); 240.
printf("<br />Sum of returned rows: %d\n", 241.
$this->rowcounter); 242.
printf("<br />Sum of query time (MySQL): %f\n", 243.
$this->dbtime); 244.
printf("<br />Processing time (PHP): %f\n", 245.
$totalTime - $this->dbtime); 246.
printf("<br />Total time since MyDB creation / last reset: %f</font></p>\n", 247.
$totalTime); 248.
} 249.
250.
function resetStatistics() { 251.
$this->sqlcounter = 0; 252.
$this->rowcounter = 0; 253.
$this->dbtime = 0; 254.
$this->starttime = $this->microtime_float(); } 255.
256.
private function microtime_float() { 257.
list($usec, $sec) = explode(" ", microtime()); 258.
return ((float)$usec + (float)$sec); 259.
} 260.
261.
} 262.
?>Viel Spaß damit
Gruß
Frank
masterG schreibt am 02.06.2009 um 18:29:11 Uhr
ich hab da auch noch eine:
aber PDO ist natürlich besser. Das ist eine alte Klasse die ich mal geschrieben habe. Mittlerweile schreib ich datenbank klassen nur mit PDO
01.
<?php 02.
03.
class mysql 04.
{ 05.
protected $sql_link; 06.
protected $last_result; 07.
protected $num_querys = 0; 08.
protected $last_sql = ''; 09.
protected $sql_array = array(); 10.
protected $sql_false = 0; 11.
protected $sql_true = 0; 12.
protected $query_time = 0; 13.
14.
public function __construct($config) 15.
{ 16.
$host = $config['db']['host']; 17.
$user = $config['db']['username']; 18.
$pass = $config['db']['password']; 19.
$db = $config['db']['database']; 20.
21.
if( !( $this->sql_link = mysql_connect($host, $user, $pass) ) ) 22.
{ 23.
$this->error(); 24.
} 25.
26.
if( !mysql_select_db($db, $this->sql_link) ) 27.
{ 28.
$this->error(); 29.
} 30.
31.
return true; 32.
} 33.
34.
public function doQuery($sql, $unbuffer = false) 35.
{ 36.
$this->num_querys++; 37.
$this->last_sql = $sql; 38.
$this->sql_array[] = $sql; 39.
40.
$start = microtime(true); 41.
42.
if( $unbuffer == true ) 43.
{ 44.
if( $result = mysql_unbuffered_query($sql, $this->sql_link) ) 45.
{ 46.
$this->last_result = $result; 47.
$this->sql_true++; 48.
} 49.
else 50.
{ 51.
$this->error(); 52.
$this->sql_false++; 53.
$result = false; 54.
} 55.
} 56.
else 57.
{ 58.
if( $result = mysql_query($sql, $this->sql_link) ) 59.
{ 60.
$this->last_result = $result; 61.
$this->sql_true++; 62.
} 63.
else 64.
{ 65.
$this->error(); 66.
$this->sql_false++; 67.
$result = false; 68.
} 69.
} 70.
71.
$this->query_time += microtime(true) - $start; 72.
73.
return $result; 74.
} 75.
76.
public function fetchrow($result = '') 77.
{ 78.
if( empty($result) ) 79.
{ 80.
$result = $this->last_result; 81.
} 82.
83.
return mysql_fetch_assoc($result); 84.
} 85.
86.
public function getOne($sql) 87.
{ 88.
return $this->fetchrow($this->doQuery($sql)); 89.
} 90.
91.
public function getAll($sql) 92.
{ 93.
$res = $this->doQuery($sql); 94.
$result = array(); 95.
96.
while($data = $this->fetchrow($res)) 97.
{ 98.
$result[] = $data; 99.
} 100.
101.
return $result; 102.
} 103.
104.
public function lastId() 105.
{ 106.
return mysql_insert_id($this->sql_link); 107.
} 108.
109.
public function escape($var) 110.
{ 111.
return '\''. mysql_real_escape_string($var, $this->sql_link) . '\''; 112.
} 113.
114.
public function get_debug_infos($strict = false) 115.
{ 116.
$debug = array(); 117.
$debug['q_time'] = $this->query_time; 118.
$debug['q_count'] = $this->num_querys; 119.
$debug['q_true'] = $this->sql_true; 120.
$debug['q_false'] = $this->sql_false; 121.
$debug['q_all'] = $this->sql_array; 122.
123.
return $debug; 124.
} 125.
126.
protected function error() 127.
{ 128.
trigger_error('[DB Error] <b>Fehler bei der Abfrage von Daten aus der Datenbank</b>: <pre>'. $this->last_sql .'</pre> Fehler: '. mysql_error($this->sql_link) .'::'.mysql_errno($this->sql_link), E_USER_NOTICE); 129.
} 130.
131.
public function __destruct() 132.
{ 133.
return mysql_close($this->sql_link); 134.
} 135.
136.
} 137.
?>aber PDO ist natürlich besser. Das ist eine alte Klasse die ich mal geschrieben habe. Mittlerweile schreib ich datenbank klassen nur mit PDO
masterG schreibt am 22.10.2011 um 09:59:30 Uhr
Nach langer Zeit hab ich mal wieder ein bisschen herumgebastelt. Um genau zu sein die gesammte Klasse über den Haufen geworfen und neu programmiert.
Das Ergebnis sieht man auf Github: http://github.com/gianluca311/phpMySQLiDa ...
Das Ergebnis sieht man auf Github: http://github.com/gianluca311/phpMySQLiDa ...









