Class: database

Source Location: /system/libraries/database.php

Class Overview [line 81]


Database class for all database queries This database class is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

Author(s):

Version:

  • 2.2.8 For full documentation and updates please visit: http://development.andrewjudd.ca Version Updates:

    2.0.0:
    This updated version of the database class uses PDO and PDO Statements as the basis for all of the database queries. This will increase the security of the script as well as the ability for the script.

    2.0.1:
    Added in %p for signed/unsigned decimal values.

    2.1.0:
    All error messages are appended onto each other before the query is complete.

    2.2.0:
    • Added the ability to track the total amount of time for all queries
    • Added a count of the number of successful queries which were run
    • Added the ability to log all queries with their associated times

    2.2.1:
    Fixed a bug that was making it so that numRows didn't actually work properly/as expected.

    2.2.2:
    • Added in %i for IP addresses
    • Added in extra error checking on the getArray function

    2.2.3:
    • Added in %b for binary files

    2.2.4:
    • Added the ability to view error messages that are sent back from the database for more information about what went wrong with the query.

    2.2.5:
    • Freed some memory when you close the cursor
    • Added the 'getInsertId' function
    2.2.6:
    • Fixed a bug which wasn't allowing a NULL username or password (changed the default values to empty)
    2.2.7:
    • Added a getNumAffected function in order to determine how many rows were affected by the query which was run.
    • Changed printLog so that it has valid HTML (inline css rather than external)
    2.2.8:
    • Added in the line number into the invalid data type messages

Copyright:

  • Andrew Judd, 2009

Variables

Constants

Methods


Inherited Variables

Inherited Constants

Inherited Methods



Class Details

Database class for all database queries This database class is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

Database class for all database queries This database class is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This database class 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 General Public License for more details.

Tags:

[ Top ]


Class Variables

$connected

[line 87]

Used to symbolise whether or not a connection to the database has been made.

Used to symbolise whether or not a connection to the database has been made.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$connection

[line 97]

Used to store the connection to the database

Used to store the connection to the database

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$errorMessages

[line 124]

Used to hold a list of any exceptions that are thrown.

Used to hold a list of any exceptions that are thrown.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$log

[line 119]

Used to hold the log of all the queries that were executed along with how long it took them to be executed.

Used to hold the log of all the queries that were executed along with how long it took them to be executed.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$logQueries

[line 113]

Used to determine if we should be logging the queries that get executed.

Used to determine if we should be logging the queries that get executed.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$queryCount

[line 108]

Used to maintain a count of the number of SUCCESSFUL queries that were executed.

Used to maintain a count of the number of SUCCESSFUL queries that were executed.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$totalTime

[line 102]

Used to store the total amount of time that the database options required

Used to store the total amount of time that the database options required

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]

$verbose

[line 92]

Used to symbolise if Verbose mode is on or off.

Used to symbolise if Verbose mode is on or off.

Tags:

  • access - private

Type: mixed

Overrides:

[ Top ]


Class Methods

__construct

database __construct( [$connInfo $connInfo = null], [$connType $connType = "mysql"])

[line 142]

This is the two argument constructor for the database connection object. if possible or if it fails it will set the 'connected' flag to false indicating that we aren't connected to the database.

This is the two argument constructor for the database connection object. This will create the database connection if possible or if it fails it will set the 'connected' flag to false indicating that we aren't connected to the database.

Tags:

  • access - public

Parameters:

  • $connInfo $connInfo - The connection information about how to connect to the database. This value should be an array setup in the following fashion. array ( "host" => "hostname", "name" => "database_name", "user" => "username", "pass" => "password" ) If this array is NULL or any of the elements are missing, then they will be defaulted to the values as specified in the constructor.
  • $connType $connType - The type of database that we are connecting to

[ Top ]

addTime

void addTime( $amount $amount)

[line 1027]

This function is used in order to add more time to the total time that the script spent on database processing.

This function is used in order to add more time to the total time that the script spent on database processing.

Tags:

  • access - private

Parameters:

  • $amount $amount - The amount of time to add to the total time spent querying the database.

[ Top ]

appendToLog

void appendToLog( $query $query, $time $time)

[line 1071]

This function is used in order to add the query to the list of all queries which were executed.

This function is used in order to add the query to the list of all queries which were executed.

Tags:

  • access - private

Parameters:

  • $query $query - The query that was executed
  • $time $time - The amount of time it took for the query to be executed

[ Top ]

commit

TRUE commit( )

[line 960]

This function is used in order to commit all of the queries which were executed on the database to the database.

This function is used in order to commit all of the queries which were executed on the database to the database.

Tags:

  • return - on success, FALSE on failure
  • access - public

Parameters:

[ Top ]

error

void error( $text $text)

[line 610]

This function is used in order to display an error message about what went wrong within the query.

This function is used in order to display an error message about what went wrong within the query.

Tags:

  • access - private

Parameters:

  • $text $text - - The text that the error messsage will display

[ Top ]

freeResults

void freeResults( $res)

[line 234]

This function is used in order to free the results from the query that is passed in as the argument.

This function is used in order to free the results from the query that is passed in as the argument.

Tags:

  • access - public

Parameters:

  • array $res - The result set to clear

[ Top ]

getArray

The getArray( $res)

[line 779]

This function is used in order to retrieve an associative array from the database statement.

This function is used in order to retrieve an associative array from the database statement.

Tags:

  • return - associative array retrieved from the database query or NULL if if value passed in was NULL
  • access - public

Parameters:

  • array $res - The result that the 'query' function returned

[ Top ]

getBoth

The getBoth( $res)

[line 833]

This function is used in order to retrieve both the associative array and the indexed array from the database statement.

This function is used in order to retrieve both the associative array and the indexed array from the database statement.

Tags:

  • return - associative array and the indexed array retrieved from the database query or NULL if value passed in was NULL
  • access - public

Parameters:

  • array $res - - The result that the 'query' function returned

[ Top ]

getDatabaseConnection

The getDatabaseConnection( )

[line 864]

This function is used in order to retrieve the actual database connection that was created on instantiation of the object.

This function is used in order to retrieve the actual database connection that was created on instantiation of the object.

Tags:

  • return - database connection object
  • access - public

Parameters:

[ Top ]

getErrorMessage

NULL getErrorMessage( $res)

[line 674]

This function is used in order to retrieve the error message which corresponds to the query that was executed.

This function is used in order to retrieve the error message which corresponds to the query that was executed.

Tags:

  • return - if there was no error present or the exception.
  • access - private

Parameters:

  • array $res - - The statement that has been executed.

[ Top ]

getInsertId

NULL getInsertId( $res)

[line 637]

This function is used in order to retrieve the insert id for the database query that has been run.

This function is used in order to retrieve the insert id for the database query that has been run.

Tags:

  • return - if the statement provided was invalid or the insert id
  • access - public

Parameters:

  • array $res - - The statement that has been executed.

[ Top ]

getLogging

TRUE getLogging( )

[line 1060]

This function is used in order to determine if we are keeping logs of all of the queries that occur.

This function is used in order to determine if we are keeping logs of all of the queries that occur.

Tags:

  • return - if we are logging all of the queries, FALSE if we are not
  • access - public

Parameters:

[ Top ]

getNumAffected

NULL getNumAffected( $res)

[line 656]

This function is used in order to retrieve the number of rows affected by the database query which was just run. of rows modified, for DELETE it returns the number of rows deleted, etc.

This function is used in order to retrieve the number of rows affected by the database query which was just run. For UPDATE it returns the number of rows modified, for DELETE it returns the number of rows deleted, etc.

Tags:

  • return - if the statement provided was invalid or the insert id
  • access - public

Parameters:

  • array $res - - The statement that has been executed.

[ Top ]

getNumber

The getNumber( $res)

[line 816]

This function is used in order to retrieve an indexed array from the database statement.

This function is used in order to retrieve an indexed array from the database statement.

Tags:

  • return - indexed array retrieved from the database query or NULL if value passed in was NULL
  • access - public

Parameters:

  • array $res - The result that the 'query' function returned

[ Top ]

getTotalTime

The getTotalTime( [$dp $dp = null])

[line 1007]

This function is used in order to retrieve the total amount of time it took to execute all of the queries on the database.

This function is used in order to retrieve the total amount of time it took to execute all of the queries on the database.

Tags:

  • return - total amount of time taken to execute all of the database queries in microseconds.
  • access - public

Parameters:

  • $dp $dp - How many decimal places to round the number to, ignore this parameter if you do not want to round the number, otherwise if invalid then NULL will be returned.

[ Top ]

getVerbose

TRUE getVerbose( )

[line 794]

This function is used in order to determine if Verbose mode is turned on or not.

This function is used in order to determine if Verbose mode is turned on or not.

Tags:

  • return - if it is FALSE if it is not
  • access - private

Parameters:

[ Top ]

numRows

NULL numRows( $res $res)

[line 754]

This function is used in order to return the number of rows for a given query.

This function is used in order to return the number of rows for a given query.

Tags:

  • return - if the parameter is invalid or the number of rows that is with the database statement
  • access - public

Parameters:

  • $res $res - The statement that has been executed.

[ Top ]

printLog

void printLog( )

[line 1090]

This function is used in order to display the log in a more useful manner.

This function is used in order to display the log in a more useful manner. This function will output the information directly onto the page.

Tags:

  • access - public

Parameters:

[ Top ]

query

This query( $query $query, $args1-X 1)

[line 299]

This function is the starting function for the query that is being run.

This function is the starting function for the query that is being run. It will take in the query with the following values for the parameters and then insert the values in their corresponding spots after making sure that the data passed in matches the correct data type (based on regular expression).

The following are parameter types for the values:

  • %u - unsigned integer - ^[0-9]+$
  • %d - signed integer - ^[-+]?[0-9]+$
  • %p - decimal number - ^[-+]?[0-9]+(\.[0-9]+)?$
  • %s - string (no HTML permitted)
  • %h - string (HTML permitted)
  • %l - list of strings (comma separated) or an array (no HTML permitted)
  • %i - IP Address - ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
  • %b - Binary Files
  • %f - field
The number of arguments passed into this function is required to be directly related to the number of parameters included in the query. For example, if there are two parameters in the query then there should be three arguments for the function (one argument for the query and two arguments for the necessary parameters). Example of how to use the parameters in a query: $db = new database (); //make the database connection
$result = $db -> query ( "SELECT * FROM `members` WHERE `id` = %u AND `username` = %s", $user_id, $username ); //Create the query
The query will validate the variable $user_id will be validated as an unsigned integer and the $username will be validated as a string.

Tags:

  • return - function will either return a PDO Statement which has been executed (if all validation is successful) or NULL NULL will be returned in the following cases:
    • No database connection
    • Too few arguments
    • Too many arguments
    • Invalid Data Type
    • Unknown Parameter Type
  • access - public

Parameters:

  • $args1-X 1 - All of the corresponding values for the query (the number of these is based on however many parameters there are in the database query)
  • $query $query - The query that we will be building on

[ Top ]

retrieveAllRows

An retrieveAllRows( $res)

[line 849]

This function is used in order to retrieve all of the rows which were obtained by the database query.

This function is used in order to retrieve all of the rows which were obtained by the database query.

Tags:

  • return - array of all of the results retrieved from the database
  • access - public

Parameters:

  • array $res - - The result that the 'query' function returned

[ Top ]

retrieveErrorMessage

Either retrieveErrorMessage( $res, [$onlyMessage $onlyMessage = TRUE])

[line 702]

This function is used in order to retrieve the error that was with a corresponding query (i.

This function is used in order to retrieve the error that was with a corresponding query (i.e. invalid SQL).

Tags:

  • return - the string containing the error message or the entire exception.
  • access - public

Parameters:

  • array $res - - The statement that has been executed.
  • $onlyMessage $onlyMessage - If the user only wants the error message returned or not. This is defaulted to only return the error message.

[ Top ]

retrieveLog

The retrieveLog( )

[line 1081]

This function is used in order to retrieve the raw version of the log (the array that actually holds the log).

This function is used in order to retrieve the raw version of the log (the array that actually holds the log).

Tags:

  • return - array containing the actual log
  • access - public

Parameters:

[ Top ]

retrieveNumberOfQueries

The retrieveNumberOfQueries( )

[line 1037]

This function is used in order to retrieve the number of queries which were executed on the database.

This function is used in order to retrieve the number of queries which were executed on the database.

Tags:

  • return - number of successful queries executed on the database
  • access - public

Parameters:

[ Top ]

rollback

TRUE rollback( )

[line 917]

This function is used in order to rollback any queries which were executed on the database.

This function is used in order to rollback any queries which were executed on the database.

Tags:

  • return - on success, FALSE on failure
  • access - public

Parameters:

[ Top ]

setErrorMessage

void setErrorMessage( $error $error, $errorInformation $errorInformation)

[line 624]

This function is used to set the error message for the specified error.

This function is used to set the error message for the specified error.

Tags:

  • access - private

Parameters:

  • $error $error - The information which will be used as the unique key to search for the error message with.
  • $errorInformation $errorInformation - The exception that has been thrown

[ Top ]

setLogging

void setLogging( $value $value)

[line 1047]

<em> This function is used in order to turn on/off the query logging functionality.

<em> This function is used in order to turn on/off the query logging functionality.

Tags:

  • access - public

Parameters:

  • $value $value - Either TRUE or FALSE depending if you want all queries to be logged (TRUE they will be, FALSE they will not be)

[ Top ]

setVerbose

void setVerbose( $verbose $verbose)

[line 804]

This function is used in order to set whether or not verbose mode is on or off.

This function is used in order to set whether or not verbose mode is on or off.

Tags:

  • access - public

Parameters:

  • $verbose $verbose - TRUE if the verbose mode is to be turned on, FALSE otherwise

[ Top ]

startTransaction

TRUE startTransaction( )

[line 874]

This function is used in order to start a transaction so that the set of queries can be rolled back or committed at any time.

This function is used in order to start a transaction so that the set of queries can be rolled back or committed at any time.

Tags:

  • return - on success, FALSE on failure
  • access - public

Parameters:

[ Top ]

validateResultSet

TRUE validateResultSet( $res)

[line 736]

This function is used internally in order to verify that the result set sent in from the user is valid.

This function is used internally in order to verify that the result set sent in from the user is valid.

Tags:

  • return - when the result set is valid FALSE otherwise
  • access - private

Parameters:

  • array $res - - The statement that has been executed.

[ Top ]


Class Constants


Documentation generated on Thu, 01 Jul 2010 20:37:17 -0400 by phpDocumentor 1.4.3