note 79360 deleted from function.mysql-fetch-row by ezyang
| From: | ezyang@php.net | Date: | Fri, 23 Nov 2007 16:06:52 +0000 |
| Subject: | note 79360 deleted from function.mysql-fetch-row by ezyang | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132512@lists.php.net to get a copy of this message | ||
Note Submitter: sebasg37 at gmail dot com
Reason: useless
----
Here you have a function that takes the fields of a mysql query and returns a list of variables with
each field's name:
function listResults($sql)
{
$fields = array();
for($i = 0; $i < mysql_num_fields($sql); $i++)
{
$meta = mysql_fetch_field($sql, $i);
$fields[$i] = $meta->name;
$$fields[$i] = "";
global $$fields[$i];
$fields[$i] = '$'.$fields[$i];
}
$fields_ = implode(", ", $fields);
eval('if(list('.$fields_.') = mysql_fetch_row($sql)) return true; else return
false;');
}
for example, if you have this "people" table:
name age country
-----------------------------
John 25 USA
Eddie 30 England
Alexi 35 Finland
Earl 50 USA
and you want to echo the country and name of all the people that has an age less or equal than 30,
you have:
$sql = mysql_query("select name, age from people where age <= 30 order by age");
while(listResult($sql))
echo "Name: $name<br>Age: $age<br><br>";
this code results on this output:
Name: John
Age: 25
Name: Eddie
Age: 30