Query() Function
query() function will accept a query as its argument and return the data in different formats based on the result of the query.
Returns
This function has five different types of returns...
One Element
If the query return one row which has just one field in the result, the function will return just the value of that field.
Example :query("SELECT user_name FROM users WHERE user_id='1'");
Query Result : Binny
One Row
If the query has only one row as the result, the function will return just that row as an associative array.
Example :query("SELECT * FROM users WHERE user_status='1'");
Query Result...
array ( 'user_id' => 1, 'user_name' => 'Binny', 'user_url' => 'http://www.bin-co.com/', 'user_status' => '1' )
All Items - Numeric
If the query return multiple rows but just one field, the function will a numeric array of all the items.
Example :query("SELECT user_name FROM users WHERE user_status='1'");
Query Result...
array ( [0] => 'Binny', [1] => 'Another Guy', [2] => 'Yet Another Guy' )
All Items - Associative
If the data returned by the query has multiple rows and two fields - in which the first is a primary key, the function return a an associative array with the primary key and the value in which the primary key will be the index.
Example :query("SELECT user_id,user_name FROM users WHERE user_status='1'");
Query Result...
array ( [1] => 'Binny', [3] => 'Another Guy', [7] => 'Yet Another Guy' )
MySQL Resource
If the returned data matches none of the above conditions, the function will return the SQL handle of the query. This is data returned by the mysql_query()
function of PHP.
query("SELECT * FROM users WHERE user_status='1'");
Query Result : Result will be a SQL resource variable.
Code
<?php
function query($query) {
if(!$query) return "";
$result = mysql_query($query) or die("MySQL Error : " . mysql_error() . "\n<br />In Query <code>$query</code>");
if(mysql_num_rows($result) == 1) { //If there is just one result, return it.
$arr = mysql_fetch_assoc($result);
if(count($arr) == 1) { //If there is just one result...
$item = array_values($arr);
return stripslashes($item[0]); // Creates the effect of 'getOne()'
} else {
foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
return $arr; // Makes the 'query()' effect
}
} else {
$arr = array();
$primary_key = false;
while ($all = mysql_fetch_row($result)) {
if(count($all) == 1) array_push($arr,$all[0]);
elseif(count($all) == 2) {
if(!$primary_key) { //Happens only one time
$meta = mysql_fetch_field($result,0); //If the first element is a primary key, make
$primary_key = $meta->primary_key; // the result an associative array.
}
//Make it an Associative Array if there is a primary_key in the given data.
if($primary_key) $arr[$all[0]] = $all[1];
else break;
}
else break;
}
if($arr) {
//Do a stripslashes() on each element
foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
return $arr; // For 'getAll()'
} else { //If nothing matches...
mysql_data_seek($result,0); //Reset the Query.
return $result; // This results in 'getSqlHandle()' effect
}
}
}