PHP Classes

File: DATA/MySQL5/Access.php

Recommend this page to a friend!
  Classes of Martin Alterisio   DATA   DATA/MySQL5/Access.php   Download  
File: DATA/MySQL5/Access.php
Role: Class source
Content type: text/plain
Description: This class provides basic interaction with a MySQL5 database.
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change: v0.8 - abstraction of sql data types and inboxing of these values into objects with constraints checking
Date: 16 years ago
Size: 5,273 bytes
 

Contents

Class file image Download
<?php
/**
 * @package DATA_MySQL5
 */

/**
 * This class provides basic interaction with a MySQL5 database.
 *
 * Access functions called from the Database object are proxied
 * to this object.
 * @see DATA_MySQL5_Database::__call()
 */
class DATA_MySQL5_Access {
   
/**
     * Connects to a MySQL5 database.
     * @link http://php.net/mysql_connect
     * @param string $server Optional. Server address.
     * @param string $username Optional. User name.
     * @param string $password Optional. Password.
     * @param bool $newLink Optional. Force a second link to be opened in the case
     * there is already a connection with the same parameters.
     * @param int $clientFlags Optional.
     * @return resource The opened connection.
     */
   
static public function connect() {
       
$args = func_get_args();
        return
call_user_func_array('mysql_connect', $args);
    }
   
   
/**
     * Changes the current schema in the currently connected MySQL5 database
     * @param string $schema The schema name.
     * @param resource $link Optional. A different link to an opened MySQL5 connection.
     * @return bool True on success, false on failure.
     */
   
static public function selectSchema($schema) {
       
$args = func_get_args();
        return
call_user_func_array('mysql_select_db', $args);
    }
   
   
/**
     * Executes a query.
     * Throws a {@link DATA_MySQL5_Error DATA_MySQL5_Error exception} on failure.
     * @param string $sql The query in sql.
     * @return resource An object/resource representing the query results.
     */
   
static public function query($sql) {
       
$query = mysql_query($sql);
        if (!
$query) throw new DATA_MySQL5_Error();
        return
$query;
    }
   
   
/**
     * Returns the last auto numeric id inserted.
     * @return int The id.
     */
   
static public function getInsertID() {
        return
mysql_insert_id();
    }
   
   
/**
     * Free the memory occupied by a query result.
     * @param resource $query An object/resource representing the query results.
     * @return bool True on success, false on failure.
     */
   
static public function freeResult($query) {
        return
mysql_free_result($query);
    }
   
   
/**
     * Returns a numerically indexed array with the next results in the
     * provided query object/resource.
     *
     * @param resource $query The query results object/resource.
     * @return array The next row in the results.
     */
   
static public function fetchRow($query) {
        return
mysql_fetch_row($query);
    }
   
   
/**
     * Returns an associative array with the next results in the
     * provided query object/resource.
     * @param resource $query The query results object/resource.
     * @return array The next row in the results.
     */
   
static public function fetchAssoc($query) {
        return
mysql_fetch_assoc($query);
    }
   
   
/**
     * Get field data from query result.
     * @param resource $query The query results object/resource.
     * @param int $row The row number.
     * @param int|string $field Optional. The field. First field if omitted.
     * @return string The field data.
     */
   
static public function result($query, $row) {
       
$args = func_get_args();
        return
call_user_func_array('mysql_result', $args);
    }
   
/**
     * Moves the result pointer.
     * @param resource $query An object/resource representing the query results.
     * @param int $row The row number.
     * @return bool True on success, false on failure.
     */
   
static public function dataSeek($query, $row) {
        return
mysql_data_seek($query, $row);
    }
   
   
/**
     * Returns the error message of the last error occurred.
     * @param resource $link Optional. Link to an open database connection.
     * @return string The error message.
     */
   
static public function error() {
       
$args = func_get_args();
        return
call_user_func_array('mysql_error', $args);
    }
   
   
/**
     * Returns the error number of the last error occurred.
     * @param resource $link Optional. Link to an open database connection.
     * @return int The error number.
     */
   
static public function errorNo() {
       
$args = func_get_args();
        return
call_user_func_array('mysql_errno', $args);
    }
   
   
/**
     * Escapes a string to be used between single quotes in a sql query.
     * @param string $string The string needing escaping.
     * @return string The escaped string.
     */
   
static public function escape($string) {
        return
mysql_real_escape_string($string);
    }
   
   
/**
     * Prepares data to be used in a sql query.
     * @param mixed $data The data.
     * @return string The string representing the data in a sql query.
     */
   
static public function prepareData($data) {
        if (
$data === null || ($data instanceof DATA_SQLType && $data->isNull())) {
            return
'NULL';
        } else {
            return
"'" . self::escape((string)$data) . "'";
        }
    }
}
?>