PHP MySQL Class
Auf drängen eines Einzelnen für die Allgemeinheit eine Warnung:
Diese Klasse dient der Veranschaulichung und sollte so nicht benutzt werden da sie ein Sicherheitsrisiko darstellt (zufrieden ‘fanta’?)
| PHP | | copy code | | ? |
| 001 | < ?php |
| 002 | |
| 003 | class MySQL { |
| 004 | |
| 005 | var $connection; |
| 006 | |
| 007 | function connect() |
| 008 | { |
| 009 | global $dbhost,$dbuser,$dbpass; |
| 010 | |
| 011 | if(!$this->connection) |
| 012 | $this->connection = mysql_connect($dbhost,$dbuser,$dbpass) OR DIE ($this->mysql_err()); |
| 013 | |
| 014 | if($this->connection) |
| 015 | return $this->connection; |
| 016 | else |
| 017 | break; |
| 018 | } |
| 019 | |
| 020 | function mysql_err() |
| 021 | { |
| 022 | global $PHP_SELF; |
| 023 | $error = mysql_errno($this->connection).": ".mysql_error($this->connection)."( ".$PHP_SELF." )\n"; |
| 024 | return $error; |
| 025 | } |
| 026 | |
| 027 | function getTable($dbname,$query) |
| 028 | { |
| 029 | global $debug; |
| 030 | if (is_numeric(strpos($query, " "))) |
| 031 | { |
| 032 | $query = $query; |
| 033 | } |
| 034 | else |
| 035 | { |
| 036 | $query = "SELECT $query.* FROM $query"; |
| 037 | } |
| 038 | |
| 039 | if($debug[MySQL] == true) |
| 040 | { |
| 041 | echo $query."\n"; |
| 042 | } |
| 043 | $result = mysql_db_query($dbname,$query,$this->connect()) OR DIE($this->mysql_err()); |
| 044 | |
| 045 | if($result) |
| 046 | { |
| 047 | $i=0; |
| 048 | while($tmp = mysql_fetch_array($result,MYSQL_ASSOC)) |
| 049 | { |
| 050 | foreach($tmp as $key=>$element) |
| 051 | { |
| 052 | $array[$i][$key]=stripslashes($element); |
| 053 | } |
| 054 | $i++; |
| 055 | } |
| 056 | } |
| 057 | |
| 058 | mysql_free_result($result); |
| 059 | |
| 060 | return $array; |
| 061 | } |
| 062 | |
| 063 | function saveTableRow($dbname,$query,$array) |
| 064 | { |
| 065 | global $debug; |
| 066 | foreach($array as $key=>$element) |
| 067 | { |
| 068 | $element = addslashes($element); |
| 069 | $keys[] = $key; |
| 070 | $values[] = "'".$element."'"; |
| 071 | $update[] = $key."='".$element."'"; |
| 072 | } |
| 073 | |
| 074 | if($array[ID] == "") |
| 075 | { |
| 076 | $query = "INSERT INTO $query (".implode(", ",$keys).") VALUES (".implode(", ",$values).")"; |
| 077 | } |
| 078 | else |
| 079 | { |
| 080 | $query = "UPDATE $query SET ".implode(", ",$update)." WHERE ID = '".$array[ID]."'"; |
| 081 | } |
| 082 | |
| 083 | if($debug[MySQL] == true) |
| 084 | { |
| 085 | echo $query."\n"; |
| 086 | } |
| 087 | $result = mysql_db_query($dbname,$query,$this->connect()) OR DIE($this->mysql_err()); |
| 088 | $id = mysql_insert_id(); |
| 089 | |
| 090 | if($id=="") |
| 091 | { |
| 092 | $id = $array[ID]; |
| 093 | } |
| 094 | if($id=="") |
| 095 | { |
| 096 | $id=false; |
| 097 | } |
| 098 | return $id; |
| 099 | } |
| 100 | |
| 101 | function deleteTableRow($dbname,$query,$id="") |
| 102 | { |
| 103 | global $debug; |
| 104 | if (is_numeric(strpos($query, " "))) |
| 105 | { |
| 106 | $query = $query; |
| 107 | } |
| 108 | elseif(is_numeric($id)) |
| 109 | { |
| 110 | $query = "DELETE FROM $query WHERE ID = '$id'"; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | $query = "DELETE FROM $query"; |
| 115 | } |
| 116 | if($debug[MySQL] == true) |
| 117 | { |
| 118 | echo $query."\n"; |
| 119 | } |
| 120 | $result = mysql_db_query($dbname,$query,$this->connect()) OR DIE ($this->mysql_err()); |
| 121 | |
| 122 | return $result; |
| 123 | } |
| 124 | |
| 125 | function executeQ($dbname,$query) |
| 126 | { |
| 127 | global $debug; |
| 128 | if($debug[MySQL] == true) |
| 129 | { |
| 130 | echo $query."\n"; |
| 131 | } |
| 132 | $result = mysql_db_query($dbname,$query,$this->connect()) OR DIE ($this->mysql_err()); |
| 133 | |
| 134 | return $result; |
| 135 | } |
| 136 | |
| 137 | function close() |
| 138 | { |
| 139 | if($this->connection) |
| 140 | mysql_close($this->connection); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | ?> |
Leider funktioniert noch kein LEFT JOIN. jedoch kann man mit executeQ nachhelfen ;)
Diese Klasse wurde aus meinen untiefen der HDD gekramt, sie wird nicht mehr weiterentwickelt und steht zur freien Verfügung.