Sources for file plugins/database/msql.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/database/database.msql.php
 *              Database Plugin - mSQL
 *
 * Version:     2.0b3
 * CVS tag:     $Id: msql.php,v 1.3 2002/10/08 17:19:54 bok Exp $
 * Author:      Robert Amos <bok[at]ausmac.net>
 *              Andrew Wellington <proton[at]wiretapped.net>
 * Copyright:   2001,2002 Shiznatz Inc.
*/

class mSQL 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"];
        }

                
// Add the host together
                
if (!empty($this->host) && !empty($this->port)) {
                        
$this->host .= $this-port;
                }

                
// if we have a host, connect, else use unix socket
                
$conn = (empty($this->host) ? @mysql_pconnect() : @msql_pconnect($this->host,
$this->username$this->password));

                
// Select database if connected, error if not
        
global $error;
                
$conn $db = @msql_select_db($database_name$conn) :
$error->error('DATABASE_ERROR'"Unable to establish connection to mSQL server.");

        
// 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 = @msql_query($sql$conn);
                if (!
$result && !$report) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR'"SQL Query failed. mSQL returned:
"
.msql_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 = @msql_fetch_array ($result_idMSQL_ASSOC);
                        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 multi-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') {
        global 
$config;

                
// fetch everything, error if necessary and its permitted
                
$result = @msql_fetch_array ($result_idMSQL_ASSOC);
                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, nothing new or fancy
                        
$result = @msql_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 msql numrows function
                
$result = @msql_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 appropriate msql affected rows function
                
$result = @msql_affected_rows ($result_id);

        if (!
$result && !$report) {
                        global 
$error;
            
$error->error('DATABASE_ERROR'"Unable to get affected rows in $result_id.");
        }
        return 
$result;
    }

}

?>


Website is Copyright © Odynia.org 2000-2005 - Xnyo is released under a BSD license.