Php Programming Code Examples
Php > Arrays Code Examples
This simple function will take a few arguments and easily set a associative
This simple function will take a few arguments and easily set a associative
array for each column in a result from a MySQL query
<?php
function setmysqlresults($resultpointer,$row = 0) {
$i=0;
$n=mysql_num_fields($resultpointer);
while($i<$n) {
$fieldname = mysql_field_name($resultpointer,$i);
$fieldresult = mysql_result($resultpointer,$row,$fieldname);
$hash[$fieldname] = $fieldresult;
$i++;
}
return $hash;
} // end setmysqlresults()
/*
usage:
$r = mysql_query("select Column1, Column2, Column3 from Table");
$i=0;
while(mysql_fetch_row($r)) {
$rs = setmysqlresults($r,$i);
print "$rs[Column1], $rs[Column2], $rs[Column3]\n";
$i++;
}
Using this function, you won't need to go through the lengthy assignment
of mysql_result() to variables.
To test for validity, use is_array(). If this function fails, it will
return a simple value. If it succeeds, it will be an associative array. For
more information on associative arrays, see the PHP3 manual for array().
*/
?>