The Coder

Read SQL Function

Original posted by: Admin on 03-01-2007

Object Oriented Programming is very useful and can save a lot of time and increase programs speed and save the programmers a lot of time and frustration.

A useful function that I constantly use in my sites is a mySQL read function. To use you simply call the function with a relevent query;


$array = readDB("SELECT * FROM foo WHERE 1=1;");

//Returns
array (
0 =>
array(
'column 1' => 'value 1 (Row 1 Col 1)',
'column 2' => 'value 2 (Row 1 Col 2)')
1 =>
array(
'column 1' => 'value 3 (Row 2 Col 1)',
'column 2' => 'value 4 (Row 2 Col 2)')
)


Before using this function you must have an active mySQL Connection.


function readDB($query) {

$result = mysql_query($query) or die(mysql_error());

if(!$result) {
return false;
} else {
$i = 0;
while($row = mysql_fetch_assoc($result)) {
foreach($row as $node => $key)
$return[$i][$node] = $key;

$i++;
}
}

mysql_free_result($result);

return $return;

}




Login to make a comment 0 Comments Found