ToDo: - Make array attributes case insensitive when possible */ /* Class: gapiDatabase Set up a database connection Parameters: database - The name of the database connectionParams - An array of parameters for the connection (optional; see below) Connection Paramaters: An array of connection parameters user - Username (defaults to mysql.default_user, or "" if empty) password - Password (defaults to mysql.default_password, or "" if empty) host - Hostname (defaults to mysql.default_host, or "localhost" if empty) auto - Should the database automatically be connected to? Defaults to true */ class gapiDatabase { var $database; var $cP; var $auto; function gapiDatabase( $database , $connectionParams = array() ) { $this->database = $database; $this->auto = $connectionParams["auto"]; unset($connectionParams["auto"]); foreach( $connectionParams as $k=>$p ) { if( ! $p && ini_get("mysql.default_" . $k) ) $p = ini_get("mysql.default_" . $k); if ( ! $p ) $p = ""; $this->cP[$k] = $p; } if( ! $this->cP["host"] ) $this->cP["host"] = "localhost"; if( ! $this->auto ) $this->connect(); } /* Function: connect Connect to the database Examples: | $connectionParams["auto"] = false; | | $database = new gapiDatabase('database_name', $connectionParams); | | if($database->connect()) | return "Connected!"; Returns: A boolean indicating if the connection was successful */ function connect( ) { //-- Connect to the host $return = @mysql_connect($this->cP["host"], $this->cP["user"], $this->cP["password"]); if (! $return) return false; //-- Pick the database $return = mysql_select_db($this->database); if (! $return) return false; //-- Everything seems to be working! return true; } /* Function: isConnected Checks if the database is currently connected Parameters: The $strict (boolean) parameter is optional. If true, it also matches the user and the host. It defaults to false, which means it just matches the current database name to $database Examples: | $database = new gapiDatabase('database_name', $connectionParams); | if( $database->isConnected() ) | echo "Currently connected!"; Returns: true - If the database is connected false - If the database is not connected */ function isConnected( $strict = false ) { $user = ""; $db = mysql_result( mysql_query("SELECT DATABASE()"), 0); if($strict) $user = mysql_result( mysql_query("SELECT USER()"), 0); if( $db == $this->database && (! $strict || ( $user == $cP["user"] . "@" . $cP["host"] ) ) ) return true; return false; } } /* Class: gapiTable Create a new table, or retrieve one from the database This does absolutely nothing... it will probably be deleted */ class gapiTable { var $table; function gapiTable( $table ) { $this->table = $table; } } /* Class: gapiRecord Create a new record, or retrieve one from the database Parameters: $table - The table name, required $params - Array of optional params, see below Optional Parameters: fetch - An array of columns to retrieve (defaults to all available columns) where - Where clause for the statement order - Order By clause for the statement limit - array([$offset,] $rowcount) CURRENTLY ALWAYS SET TO "array(1)" autoAdd - Boolean; if true, nonexistant tables or columns will be added automatically (Defaults to true) autoSave - Boolean, if true the record will be updated on every set() call (Defaults to false, so you must call "save()") */ class gapiRecord { var $table; var $params; var $resultsInitial; var $results; function gapiRecord( $table, $params = array() ) { $this->table = $table; $this->params = $params; $this->resultsInitial = array(); $this->results = array(); //-- Get the results if ( $params["where"] ) { $q = "SELECT " . ( (count($fetch) > 0) ? implode(", ", gapiTools::toArray($fetch)) : "*" ); $q .= " FROM " . $table . " "; $q .= ($params["where"]) ? ("WHERE " . $params["where"]) : " "; $q .= ($params["order"]) ? ("ORDER BY " . $params["where"]) : " "; $q .= "LIMIT 1"; $query = mysql_query( $q ); if (! $query) return false; $this->resultsInitial = mysql_fetch_assoc($query); $this->results = $this->resultsInitial; } } /* Function: get Get the attribute(s) requested Parameters: $attribute - The name of a column (or an array of column names) to be returned Returns: string - If only one attribute is requested array - If multiple attributes are requested Example: | $array = array( value1 => 'Value 1', value2 , value3 => 3); */ function get( $attribute ) { $return = array(); foreach( gapiTools::toArray( $attribute ) as $a) $return[$a] = $this->results[$a]; if(count($return) == 1) $return = array_shift( $return ); return $return; } /* Function: set Set the attribute(s) passed into the function Parameters: $attribute - The name of a single attribute OR (if using just the $attribute parameter) an associative array of the column name(s) and value(s) $value - (optional) If you want to set just one attribute, you can use this parameter (see below) Example (Single attribute): | $record->set('name', 'Michael'); Example (Multiple attributes): | $record->set(array('name'=>'Michael', 'phone'=>'987.555.1234') ); Returns: boolean - Will always be true unless autoSave is on; then it will return if the query was successful */ function set( $attribute, $value = false ) { if(! $value) $attribute = gapiTools::toArray($attribute); else $attribute = array($attribute => $value); foreach( $attribute as $k=>$a ) $this->results[$k] = $attribute[$k]; if($params["autoSave"]) return $this->save(); return true; } /* Function: save Save the record to the database Returns: boolean - The result of the saving */ function save( ) { //-- Should this only update changed records? //-- Make sure everything exists in the database, add if it doesnt foreach( gapiTools::toArray($this->results) as $k=>$a ) { $update .= ", `" . $k . "`='" . $a . "'"; $insertC .= ", `" . $k . "`"; $insertV .= ", '" . $a . "'"; } $update = substr($update, 2); $insertC = substr($insertC, 2); $insertV = substr($insertV, 2); $result = false; if( $this->params["where"] ) { $result = mysql_query("UPDATE " . $this->table . " SET " . $update . " WHERE " . $this->params["where"] . " LIMIT 1"); if(mysql_affected_rows() == 0) $result = false; } if (! $result ) return mysql_query("INSERT INTO " . $this->table . " (" . $insertC . ") VALUES (" . $insertV . ")"); return true; } } ?>