PHP Snippets: get mysql_query results in to array()


Array() is like little database.
How big array() could or should be – ask google.

Example of use MF_sql_fetch(name, condition) function:
MF_sql_fetch ( “tablename”  ,  “cellname LIKE %find% LIMIT 10 ACS” );

name = selected DB table name
condition = everything what goes after SELECT * WHERE …

a tip: use 1=1 for condition for collecting all records with no condition.
Which would be same as: SELECT * FROM tablename WHERE 1=1

Returns: $tablename[cellname][key]
either see visual output: print_r($tablename);

<?php

// ------------------------------------------------------------------
//
// FUNCTION MF_sql_fetch($tablename, $condition)
// RETURN ALL OF CELLS IN MULTI-DIMENSIONAL ARRAY IN FOLLOW
//
// $tablename['cellname'][0...n] = "value"...
// ------------------------------------------------------------------


function MF_sql_fetch($tablename, $condition){

	global $$tablename;
	
	$query = mysql_query("
	SELECT * FROM $tablename WHERE $condition
	") or die(mysql_error()); 
	
	// TOTAL FIELDS AND ROWS
	$f_to = mysql_num_fields($query);
	$n_to = mysql_num_rows($query);
	
	// EXIT PROCES IF NO RESULTS:
	if (!mysql_num_rows($query)) return false;
	
	//========= PUTTING ALL CELL NAMES TO $cellname ARRAY
	
	for ($f=0; $f<=$f_to-1; $f++){
		$cellname[$f] = mysql_field_name($query, $f);
	}
	
	//========= GENERATING RESULT VALUES 
	for ($n=0; $n<=$n_to-1; $n++){
		foreach ($cellname as $val){
			$result[$val][$n] = mysql_result($query, $n, $val);
		}
	}
	
	$$tablename = $result;
	
	return $$tablename;

}//function MF_sql_fetch

?>