note 79360 added to function.mysql-fetch-row
| From: | sebasg37 at gmail dot com | Date: | Thu, 22 Nov 2007 19:52:37 +0000 |
| Subject: | note 79360 added to function.mysql-fetch-row | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132491@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 190.19.15.248
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-row.php
Edit -- https://master.php.net/note/edit/79360
Del: integrated -- https://master.php.net/note/delete/79360/integrated
Del: useless -- https://master.php.net/note/delete/79360/useless
Del: bad code -- https://master.php.net/note/delete/79360/bad+code
Del: spam -- https://master.php.net/note/delete/79360/spam
Del: non-english -- https://master.php.net/note/delete/79360/non-english
Del: in docs -- https://master.php.net/note/delete/79360/in+docs
Del: other reasons-- https://master.php.net/note/delete/79360
Reject -- https://master.php.net/note/reject/79360
Search -- https://master.php.net/manage/user-notes.php