Sources for file plugins/database/mysql.php in version 2.0 Beta 4
Click on a comment to hide it. Click here to show all comments.
<?PHP
/*
* Project: Onyx: PHP Application Backend
* File: plugins/class/database.mysql.php
* Database Plugin - MySQL
*
* Version: 1.0
* CVS tag: $Id: mysql.php,v 1.2 2002/09/09 14:38:09 bok Exp $
* Author: Robert Amos <bok[at]ausmac.net>
* Andrew Wellington <proton[at]wiretapped.net>
* Copyright: 2001,2002 Shiznatz Inc.
*/
class MySQL extends Database {
/**** Open Persistant connection to the database ****/
/*
* Syntax:
* $this->_connect(string database_name);
*
* Arguments:
* database name: String with the name of the database to connect to
*
* Returns:
* database resource id
*/
function _connect ($database_name) {
// if we already have a connection open, use it
if (isset($this->connection["$database_name"])) {
return $this->connection["$database_name"];
}
// if we've specified a host, use it, else use localhost
$$this->host = (empty($this->host) ? "localhost" : $this->host);
// add a port if specified
if (isset($this->port)) {
$this->host .= ":".$this->port;
}
if (empty($this->username) || empty($this->password)) {
global $error;
$error->error('PARAM_ERROR', "Username and/or Password not specified for
MySQL Database Connect");
// Open connection
$conn = @mysql_pconnect($this->host, $this->username, $this->password);
// Select the database we want if the connection opened, else error
$conn ? $db = @mysql_select_db($database_name, $conn) :
$error->error('DATABASE_ERROR', "Unable to establish connection to mySQL server $this->host.");
// set the resource id into the database for later use
$this->connection["$database_name"] = $conn;
// return it so the calling function can use it
return $this->connection["$database_name"];
}
/**** Execute the given sql query ****/
/*
* Syntax:
* $database->exec(string sql, string database_name [, bool report]);
*
* Arugments:
* sql: String containing the SQL Query
* database_name: Database to run the SQL Query against
* report: optional boolean value to tell whether to report errors to the logs or not
*
* Returns:
* Resource id for the query
*/
function exec ($sql, $database_name, $report = '1') {
// Open connection, or get the resource id of an already open one
$conn = $this->_connect($database_name);
// exec the sql query, error if there is one and we're allowed
$result = @mysql_query($sql, $conn);
if (!$result && !$report) {
global $error;
$error->error('DATABASE_ERROR', "SQL Query failed. mySQL returned:
".mysql_error($conn));
}
// return the result index
return $result;
}
/**** Fetch the results of a select to an array ****/
/*
* Syntax:
* $database->fetch_array(int result_id, int row [, bool report]);
*
* Arguments:
* result_id: a result index, as returned by $database->exec()
* row: integer containing the row number to return
* report: optional boolean value specifing whether to report errors or not
*
* Returns:
* false: on error
* an array containing the requested row from the result specified
*/
function fetch_array ($result_id, $row, $report = '1') {
// if there are no rows, dont do anything
if ($this->numrows($result_id) != "0") {
// fetch the array, report errors if we have to
$result = @mysql_fetch_assoc ($result_id);
if (!isset($result[$row]) && !$report) {
global $error;
$error->error('DATABASE_ERROR', "Unable to fetch row $row from
$result_id.");
}
// we only want the one row, return it instead of a mulit-dimensional array
containing all rows
return $result[$row];
}
// no rows, cant return anything then can we?
return false;
}
/**** Fetch results of a select into a multi-dimensional array containing row number and columns
***/
/*
* Syntax:
* $database->fetch_all(int result_id [, bool report]);
*
* Arguments:
* result_id: a result index, as returned by $database->exec()
* report: optional boolean value specifing whether to report errors or not
*
* Returns:
* false: on error
* a multi-dimensional array containing the row number and columns
*/
function fetch_all ($result_id, $report = '1') {
// fetch the whole lot, error if we need to
$result = @mysql_fetch_assoc ($result_id);
if (!$result && !$report) {
global $error;
$error->error('DATABASE_ERROR', "Unable to fetch all from $result_id.");
}
return $result;
}
/**** Return a single result (row and column) from a select statement ****/
/*
* Syntax:
* $database->result(int result_id, int row, string cell_name [, bool report]);
*
* Returns:
* result_id: a result index, as returned by $database->exec()
* row: integer containing the row number to return
* cell_name: name of the column to fetch the result from
* report: optional boolean value specifing whether to report errors or not
*
* Returns:
* false: on error
* string containing the results of the specified cell
*/
function result ($result_id, $row, $cell_name, $report = '1') {
// if there is no rows, dont return any results, obviously
if ($this->numrows($result_id) != "0") {
// get result
$result = @mysql_result ($result_id, $row, $cell_name);
if (!$result && !$report) {
global $error;
$error->error('DATABASE_ERROR', "Unable to retrieve contents of
$cell_name, row $row, $result_id.");
}
return $result;
}
return false;
}
/**** Return the number of rows a select statement retrieved ****/
/*
* Syntax:
* $database->numrows(int result_id [, bool report]);
*
* Arguments:
* result_id: a result index, as returned by $database->exec()
* report: optional boolean value specifing whether to report errors or not
*
* Returns:
* false: on error
* an integer containing the number of results returned
*/
function numrows ($result_id, $report = '1') {
// use the mysql numrows function
$result = @mysql_num_rows ($result_id);
// return error if any of the above calls failed
if (!$result && !$report) {
global $error;
$error->error('DATABASE_ERROR', "Unable to get number of rows in $result_id.");
}
// return the number of rows
return $result;
}
/**** Return the number of rows affected by the executed statement ****/
/*
* Syntax:
* $database->affectedrows(int result_id [, bool report]);
*
* Arguments:
* result_id: a result index, as returned by $database->exec()
* report: optional boolean value specifing whether to report errors or not
*
* Returns:
* false: on error
* an integer containing the number of rows affected
*/
function affectedrows ($result_id, $report = '1') {
// use the mysql affected rows function
$result = @mysql_affected_rows ($this->connection);
if (!$result && !$report) {
global $error;
$error->error('DATABASE_ERROR', "Unable to get affected rows in $result_id.");
}
return $result;
}
}
?>
