<?php
/*----------------------------------------------------------------------------*
* xDatabase v1.0.4 - MySQL Database Class, Last Updated 23/07/2004 *
* Copyright (c) 2004 Kimmo Lankila, All Rights Reserved. *
* *
* Description : Class for simple access to MySQL databases. *
* Update query information automatically in class variables. *
*----------------------------------------------------------------------------*
* 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.1 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 *
*----------------------------------------------------------------------------*
* Class Constructor : xDatabase(string $hostname, string $username, *
* string $password, string $database) *
* *
* Class Functions : query(string $query, $index = null) *
* querySingle(string $query) *
* clean() *
* close() *
*----------------------------------------------------------------------------*/
class xDatabase
{
var $DB_HOSTNAME = "";
var $DB_USERNAME = "";
var $DB_PASSWORD = "";
var $DB_DATABASE = "";
var $DB_CONNECTION = false;
var $DB_RESULT = false;
var $DB_RESULT_ROWS = 0;
var $DB_AFFECTED_ROWS = 0;
var $DB_PREV_INSERT_ID = 0;
/*----------------------------------------------------------------------------*
* Description : Class constructor. *
* Parameters : string $hostname = MySQL server. *
* string $username = MySQL user. *
* string $password = MySQL user's password. *
* string $database = MySQL database. *
* Return : *
*----------------------------------------------------------------------------*/
function xDatabase($hostname, $username, $password, $database)
{
$this->DB_HOSTNAME = $hostname;
$this->DB_USERNAME = $username;
$this->DB_PASSWORD = $password;
$this->DB_DATABASE = $database;
$this->DB_CONNECTION = @mysql_connect($this->DB_HOSTNAME, $this->DB_USERNAME, $this->DB_PASSWORD);
if (!$this->DB_CONNECTION)
die("Unable to connect to MySQL server!");
if (!@mysql_select_db($this->DB_DATABASE))
die("Unable to select Database!");
}
/*----------------------------------------------------------------------------*
* Description : Make a MySQL query and return possible results set. *
* Parameters : string $query = Query to process. *
* bool $index = column name, which is result array's index. *
* Return : Returns TRUE or RESULT ARRAY on success, FALSE on failure. *
* NOTE : Returns Two Dimensional Associative Array of results, *
* if query type is "SELECT". *
* *
* $DB_RESULT = Results from "SELECT" query. *
* $DB_RESULT_ROWS = Number of rows in "SELECT" results. *
* $DB_PREV_INSERT_ID = ID of last row from previous "INSERT". *
* $DB_AFFECTED_ROWS = Number of affected rows in "INSERT", *
* "DELETE", or "UPDATE" query. *
*----------------------------------------------------------------------------*/
function query($query, $index = null)
{
if (!$this->DB_CONNECTION)
return false;
if (!$this->DB_RESULT = @mysql_query($query, $this->DB_CONNECTION))
return false;
switch(strtoupper(substr($query, 0, 6)))
{
case "SELECT" : $this->DB_RESULT_ROWS = @mysql_num_rows($this->DB_RESULT);
while ($db_result = mysql_fetch_array($this->DB_RESULT, MYSQL_ASSOC))
{
if (is_null($index))
$db_data[] = $db_result;
else
$db_data[$db_result[$index]][] = $db_result;
}
if (is_array($db_data))
{
return $db_data;
}
else
return false;
break;
case "INSERT" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
$this->DB_PREV_INSERT_ID = @mysql_insert_id();
break;
case "DELETE" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
break;
case "UPDATE" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
break;
}
return true;
}
/*----------------------------------------------------------------------------*
* Description : Make a MySQL query and return possible results set. *
* Parameters : string $query = Query to process. *
* Return : Returns TRUE or RESULT ARRAY on success, FALSE on failure. *
* NOTE : Returns Single Dimensional Associative Array of results, *
* if query type is "SELECT". *
* *
* $DB_RESULT = Results from "SELECT" query. *
* $DB_RESULT_ROWS = Number of rows in "SELECT" results. *
* $DB_PREV_INSERT_ID = ID of last row from previous "INSERT". *
* $DB_AFFECTED_ROWS = Number of affected rows in "INSERT", *
* "DELETE", or "UPDATE" query. *
*----------------------------------------------------------------------------*/
function querySingle($query)
{
if (!$db_result = $this->query($query))
return false;
return $db_result[0];
}
/*----------------------------------------------------------------------------*
* Description : Clean results. *
* Parameters : *
* Return : Returns TRUE on success, FALSE on failure. *
*----------------------------------------------------------------------------*/
function clean()
{
if ((!$this->DB_CONNECTION) || (!$this->DB_RESULT))
return false;
if (!(@mysql_free_result($this->DB_RESULT)))
return false;
$this->DB_RESULT = false;
$this->DB_RESULT_ROWS = 0;
$this->DB_AFFECTED_ROWS = 0;
$this->DB_PREV_INSERT_ID = 0;
return true;
}
/*----------------------------------------------------------------------------*
* Description : Close connection to MySQL database. *
* Parameters : *
* Return : Returns TRUE on success, FALSE on failure. *
*----------------------------------------------------------------------------*/
function close()
{
if (!$this->DB_CONNECTION)
return false;
if (@mysql_close($this->DB_CONNECTION))
{
$this->DB_RESULT = false;
$this->DB_CONNECTION = false;
$this->DB_RESULT_ROWS = 0;
$this->DB_PREV_INSERT_ID = 0;
return true;
}
return false;
}
}
?>