Source for file mySQL.class.php
Documentation is available at mySQL.class.php
* This class handles connections to a mysql database.
* The class is constructed to make it easy for someone to handle a mysql database
* @version 0.2, 05.02.2002
* @modified_by Eric Lamb [eric@ericlamb.net]
* TRUE if you want to be in debug-mode
* DO NOT CHANGE THESES VARIABLES
* Command to set the table lock for READ
define("LOCKED_FOR_READ", "READ");
* Command to set the table lock for WRITE
define("LOCKED_FOR_WRITE", "WRITE");
* The connection resource id
* The result from a select-query
* Flag that tells if you are connected to the database or not
* Flag that tells if you the tables are locked or not
*This will indicate what querytype the last query was
* This is the constructor of this mysql class.
* It creates a connection to the database, and if possible it sets the database to
* You can specify if you want to use persistant connections or not.
* @param string The host to the mySQL server
* @param string The username you use to log on to the mySQL server
* @param string The password you use to log on to the mySQL server
* @param string The name of the database you wish to use
* @param boolean TRUE if you want to use persistant connections. Default is TRUE
* @return boolean TRUE when connection was successfull
function __construct($sHost, $sUser, $sPassword, $sDatabase= "", $bPersistant= TRUE) {
$this->setDb($sDatabase);
* This is the destructor of this class. It frees the result of a query,
* it unlocks all locked tables and close the connection to the database
* It does not return anything at all, so you will not know if it was sauccessfull
* This function frees the result from a query if there is any result.
* This function executes a query to the database.
* The function does not return the result of the query, you must call the
* function getQueryResult() to fetch the result
* @param string The query-string to execute
* @param bool Whether to ignore errors if encoutered. Good for catching duplicate errors and not logging them
* @return boolean TRUE if the query was successfull
function query($query, $IgnoreErrors = FALSE) {
$this->printError("No query got in function query()");
$this->printError("Not connected in function query()");
$this->setQueryType($queryType);
if($this->result || $IgnoreErrors) {
return $this->query($sql);
* Sets the querytype of the last query executed
* For example it can be SELECT, UPDATE, DELETE etc.
function setQueryType($type) {
function getQueryType() {
* Returns the latest insert_id
* This function returns number of rows got when executing a query
* @return mixed FALSE if there is no query-result.
* If the queryType is SELECT then it will use the function MYSQL_NUM_ROWS
* Otherwise it uses the MYSQL_AFFECTED_ROWS
print ("<font style=\"background-color: red\">". $this->getQueryType(). "</font><br>");
* The function returns the result from a call to the query() function
* This function returns the query result as an array for each row in the query result
* This function returns the query result as an object for each row in the query result
* This function returns the query result as an array for each row in the query result
* This function sets the database
* @return boolean TRUE if the database was set
function setDb($sDatabase) {
$this->printError("Not connected in function setDb()");
* This function returns a flag so you can see if you are connected to the database
* @return boolean TRUE when connected to the database
* This function sets the flag so you can see if you are connected to the database
* @param $bStatus The status of the connection. TRUE if you are connected,
* The function unlocks tables if there are locked tables and the closes the
* connection to the database.
* Unlocks all tables that are locked
$this->query("UNLOCK TABLES");
* This function locks the table(s) that you specify
* The type of lock must be specified at the end of the string.
* @param string a string containing the table(s) to lock,
* as well as the type of lock to use (READ or WRITE)
* at the end of the string
* @return boolean TRUE if the tables was successfully locked
function lock($sCommand) {
if($this->query("LOCK TABLE ". $sCommand)) {
* This functions sets read lock to specified table(s)
* @param string a string containing the table(s) to read-lock
* @return boolean TRUE on success
* This functions sets write lock to specified table(s)
* @param string a string containing the table(s) to read-lock
* @return boolean TRUE on success
* Sets the flag that indicates if there is any tables locked
* @param boolean The flag that will indicate the lock. TRUE if locked
* Returns TRUE if there is any locked tables
* @return boolean TRUE if there are locked tables
* Returns TRUE if there is any locked tables
* @return boolean TRUE if there are locked tables
* Alias for escapeString()
* @return boolean TRUE if there are locked tables
* Prints an error to the screen. Can be used to kill the application
* @param string The text to display
* @param boolean TRUE if you want to kill the application. Default is FALSE
log_error($text, "mysql", __LINE__ , __FILE__ );
* Returns any mysql-error number
* @return mixed String with the error if there is any error.
* Otherwise it returns FALSE
* Returns any mysql-error
* @return mixed String with the error if there is any error.
* Otherwise it returns FALSE
|