* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class Database { var $fd = null; function Database($host, $username, $password, $dbname){ $this->fd =& mysql_pconnect($host, $username, $password) or die(mysql_error()); mysql_select_db($dbname,$this->fd) or die(mysql_error()); } function close(){ return mysql_close($this->fd); } function &query($sql){ print($sql."\n"); $rs =& mysql_query($sql) or die(mysql_error()); return $rs; } function queryArray($sql){ return mysql_fetch_assoc($this->query($sql)); } function exec($sql){ $this->query($sql); } /** * @return bool - Was the saved array new or did it matched an existing one? */ function saveArray($table, $arr){ $atoms = array(); foreach($arr as $k => $v){ $atoms[] = $k."='".$v."'"; } $sql = ""; $new = !$arr['id'] || $arr['id'] == null; if($new){ // INSERT unset($arr['id']); $sql = "INSERT INTO ".$table." SET ".implode(',',$atoms).";"; }else{ // UPDATE $sql = "UPDATE ".$table." SET ".implode(',',$atoms)." WHERE id='".$arr['id']."';"; } $this->exec($sql); return $new?mysql_insert_id():null; } function &queryAllArray($sql){ $res = array(); $rs =& $this->query($sql); while($t = mysql_fetch_assoc($rs)) $res[] = $t; return $res; } } ?>