MySQL PHP Class Functions
Below is the list of functions and their respective explanations for this class.
Contents |
mysqlClass function
This function is the constructor function. This function is used to instantiate a new MySQL class object and make a connection to the selected database.
function mysqlClass($h,$u,$p,$d) {
if($this->c = @mysql_connect($h,$u,$p)) {
return $this->selectdb($d);
}
$this->errors();
return false;
}
Parameters
- MySQL Host (required) - This will most likely be set to "localhost" but could be an I.P. address.
- MySQL Username (required) - This is the username you intend to connect to your database with.
- MySQL Password (required) - This is the password you intend to connect to your database with.
- MySQL Database (required) - This is the database you intend to connect to.
Return Value
Returns TRUE upon successful connection and FALSE on failure.
Examples
This function is called when you create the MySQL class object like so:
$connection = new mysqlClass($mysql_address,$mysql_username,$mysql_password,$mysql_database);
query function
This function is used to make a SELECT query on your object's database.
function query($q,$t=true,$a=false) {
$q = $this->records($q,$t);
if(!$q and $a) {
return array();
}
return $q;
}
Parameters
- MySQL Query str (required) - This is the query you'd like to perform.
- Type bool (optional) - TRUE by default, if set to TRUE this parameter will cause the function to return all records that match your query as a multi-dimensional associative array. If set to FALSE, this parameter will cause a return of the first record matching your query as an associative array.
- Force an array? "bool" (optional) - FALSE by default, if set to FALSE this parameter will cause the function to return FALSE if no records match your query. If set to TRUE, this parameter will cause an empty array to be returned. NOTE: This is important if you intent to use a foreach loop directly after a query. Set to TRUE if you wish to avoid a fatal error.
Return Value
Returns a record set if parameter two is set to TRUE and there are records returned by your query. Returns the first single record returned by your query if parameter two is set to FALSE and there is at least one record returned by your query. Returns FALSE if parameter three is set to FALSE and no records are returned by your query. Returns an empty array if parameter three is set to TRUE and no records are returned by your query.
Examples
In this example $result will be all the records from the table "table_name". If there are no records in this table $result will be FALSE.
$result = $connection->query('SELECT * from table_name');
var_dump($result);
# the above will output
array(2) {
[0] => array(2) {
["id"] => string(4) "1000"
["name"] => string(16) "John Jingle"
}
[1] => array(2) {
["id"] => string(4) "1000"
["name"] => string(16) "John Jingle"
}
}
In the following example $result will be the first record returned from the table "table_name". If there are no records in this table $result will be FALSE.
$result = $connection->query('SELECT * from table_name',false);
var_dump($result);
# the above will output
array(1) {
[0] => array(2) {
["id"] => string(4) "1000"
["name"] => string(16) "John Jingle"
}
}
In the next example $result will be an empty array. Please notice the "LIMIT 0".
$result = $connection->query('SELECT * from table_name LIMIT 0',true,true);
update function
This function is used to make UPDATE, INSERT and DELETE queries on your object's database. This function will work for any query you wish to perform that does not require a returned record set.
function update($q) {
if(@mysql_query($q)) {
return true;
}
$this->errors();
return false;
}
Parameters
- MySQL Query str (required) - This is the query you'd like to perform.
Return Value
Returns TRUE upon successful query and FALSE on failure.
Examples
This example shows a simple INSERT statement performed.
$connection->update("INSERT INTO table_name (name,phone) VALUES ('John Jingle','123-456-7890')");
disconnect function
This function is used to disconnect from your database.
function disconnect() {
if(@mysql_close($this->c)) {
return true;
}
return false;
}
Parameters
No parameters for this function.
Return Value
Returns TRUE upon successful disconnect and FALSE on failure.
Examples
This function is called when you create the MySQL class object like so:
$connection->disconnect();
select function
This function is used to compile custom SELECT queries from scratch and perform them. This is the most complicated function provided. See Compiling a query.
Parameters
- Values array (required) - This is an associative array of values which will be used to compile your query.
- Table str (required) - This is the name of the table you'd like to query.
Acceptable Values
- Any table column name and its respective value. See the examples below for a more in depth look into this option.
- limit - This value should be how many records would you like to return.
- offset - At which index would you like your returned record set to start.
- orderby - This should be which table column would you like to order your results by.
- order - This will determine ascending or descending order. (asc or desc).
- excludes - This should be a multi-dimensional associative array of values by which you'd like to exclude from your results.
Return Value
Returns a record set if any records are found and FALSE if no records are returned.
Examples
The following is a very simple example of the select function.
$connection->select(array( 'id' => 1000 ),'table_name');
This example shows how to select a range of id's in between 1000 and 2000 starting at the 10th record found limited to 100 records and excluding any record with an 'id' of 1100 or 1101.
$connection->select(array( 'id' => 'range:1000-2000', 'limit' => 100, 'offset' => 10, 'excludes' => array( 'id' => array(1100,1101) ) ),'table_name');
This next example shows how to select any records that have an 'id' greater than 1000, a name which contains the word 'John', a name which does not contain the word 'Jingle', a value of less than 200 in the 'loggedin' column. Also the record set will start at the 10th record found, will be limited to 100 records and will exclude any record with an 'id' of 1100 or 1101.
$connection->select(array( 'id' => 'greater:1000', 'name' => 'contains:John&no:Jingle', 'loggedin' => 'lesser:200', 'limit' => 100, 'offset' => 10, 'excludes' => array( 'id' => array(1100,1101) ) ),'table_name');