Opensource -> Pinnwand * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // Klasse zum Zugriff auf eine (My)SQL-Datenbank class SQLQuery { var $query; // Querystring var $handle; // Datenbank-Handle function __construct($q,// string: SQL-Query $sqlh, // string: Datenbank-Host $sqld, // string: SQL-Datenbank $sqlu, // string: Benutzername $sqlp) // string: Passwort { $this->query = $q; $this->connect($sqlh, $sqld, $sqlu, $sqlp); } /* Diese Methode stellt eine persistente Verbindung zur * Datenbank $db auf $host her und meldet $user mit $pass dort * an. Im Fehlerfall (kein connect oder select_db moeglich) * wird ein E_USER_ERROR getriggert. */ function connect($host, // string: Datenbank-Host $db, // string: SQL-Datenbank $user, // string: Benutzername $pass) // string: Passwort { if (! $this->handle = new mysqli($host, $user, $pass, $db)) trigger_error('Datenbankzugriff nicht moeglich', E_USER_ERROR); } /* Mit dieser Funktion sollten Abfragen praepariert werden, * da dies eine bequeme Moeglichkeit darstellt, Werte von * ausserhalb in die Abfrage einzusetzen. * * Die Ersetzungsvariablen werden dabei mit Anfuehrungszeichen * begrenz, d.h. aus "Name" wird 'Name', wobei Name escaped ist. * * Beispiel: * $q = new SQLQuery('SELECT * FROM tab WHERE id="N"'); * $q->bind(array('N' => 23)); * echo $q->query() * * Ausgabe: * SELECT * FROM tab WHERE id='23' */ function bind($keyval /* array */) { foreach ($keyval as $key => $val) { $val = '\''.mysql_real_escape_string($val, $this->handle).'\''; $this->query = str_replace("\"$key\"", $val, $this->query); } } /* Fuehrt den gespeicherten Query aus und gibt das Ergebnis * zurueck (Resource oder FALSE). */ function exec() { return @mysql_query($this->query, $this->handle); } // Gibt einen Datensatz fuer die Resource $req zurueck. function fetch($req /* resource */) { return @mysql_fetch_array($req); } // Gibt den intern gespeicherten SQL-Query zurueck. function query() { return $this->query; } /* Gibt die letzte Fehlermeldung fuer die vom SQLQuery-Objekt * benutzte Verbindung zurueck. */ function error() { return @mysql_error($this->handle) . ' ' . $this->query; } /* Ueberprueft, ob eine die letzte SELECT-Abfrage eine bestimmte * Anzahl an Ergebnissen lieferte. * * Diese Methode ist im Zusammenhang mit der Verwendung von * LIMIT in SELECT-Abfragen sehr praktisch, denn hiermit laesst * sich ueberpruefen, ob es noch weitere Datensaetze gibt. */ function rows($req, // resouce: Ergebniskennung, $r) // int: Anzahl der Datensaetze { return @mysql_num_rows($req) == $r; } } ?>