MySQL PHP Class Querying

There are multiple ways to query a MySQL database with this class. Please refer to the functions below. Also, see Compiling a query.

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')");
Copyright © 2007 Ternstyle Inc., All rights reserved.
Site designed by, well, me.