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.
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:
- author - Andrew Judd <andrew@andrewjudd.ca>
- version -
2.2.5
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
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
- Added in %b for binary files
- Added the ability to view error messages that are sent back from the
database for more information about what went wrong with the query.
- Freed some memory when you close the cursor
- Added the 'getInsertId' function
- Added the ability to track the total amount of time for all queries
- copyright - Andrew Judd, 2010
- license - GPLv3
[ Top ]
Class Variables
Class Methods
__construct
database __construct(
[$connInfo
$connInfo = null], [$connType
$connType = "mysql"])
[line 129]
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 ]
commit
TRUE commit(
)
[line 924]
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 ]
freeResults
void freeResults(
$res
$res)
[line 221]
This function is used in order to free the results from the query that is passed in as the argument.
Tags:
- access - public
Parameters:
- $res $res - The result set to clear
[ Top ]
getArray
The getArray(
$res
$res)
[line 743]
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:
- $res $res - The result that the 'query' function returned
[ Top ]
getBoth
The getBoth(
$res
$res)
[line 797]
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:
- $res $res - The result that the 'query' function returned
[ Top ]
getDatabaseConnection
The getDatabaseConnection(
)
[line 828]
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 ]
getInsertId
NULL getInsertId(
$res
$res)
[line 620]
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:
- $res $res - - The statement that has been executed.
[ Top ]
getLogging
TRUE getLogging(
)
[line 1024]
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 ]
getNumber
The getNumber(
$res
$res)
[line 780]
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:
- $res $res - The result that the 'query' function returned
[ Top ]
getTotalTime
The getTotalTime(
[$dp
$dp = null])
[line 971]
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 ]
numRows
NULL numRows(
$res
$res)
[line 718]
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 1054]
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 286]
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
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
$res)
[line 813]
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:
- $res $res - The result that the 'query' function returned
[ Top ]
retrieveErrorMessage
Either retrieveErrorMessage(
$res
$res, [$onlyMessage
$onlyMessage = TRUE])
[line 666]
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:
- $res $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 1045]
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 1001]
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 881]
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 ]
setLogging
void setLogging(
$value
$value)
[line 1011]
<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 768]
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 838]
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 ]