function make_vars($result)
{
$fields = mysql_num_fields($result);
if ($myrow = mysql_fetch_array($result)){
for ($i=0; $i < $fields; ++$i){ // >
$names = mysql_fieldname($result,$i);
global $$names;
$$names = $myrow[$names];
}
return true;
}else{
return false;
}
}
This is just a jump-start towards your goal. Just pass a result (not the
resulting array) and it will both fetch the array and convert it to global
variables. This is free software code - unfortunately I have no copyright data
on it, so please drop me a line if you know who the author is (the function is
included in a software product we're due to release and I'd like to include the
proper credentials if possible).
HTH
Bogdan
Justin French wrote:
> Hi all,
>
> I've got into the habbit of pulling data out of a table something like this:
>
> $sql = "SELECT * FROM cd_review WHERE publish='yes' ORDER BY id DESC
> LIMIT 3";
> $sql_result = mysql_query($sql);
> print mysql_error();
> while ($sql_myrow = mysql_fetch_array($sql_result))
> {
> // pull out the data
> $id = $sql_myrow["id"];
> $date = $sql_myrow["date"];
> $user_id = $sql_myrow["user_id"];
> $artist = $sql_myrow["artist"];
> $title = $sql_myrow["title"];
> // edited out about 10 colums for clarity
> }
>
> Now, I reckon there must be a way of automating the task of making the
> $title var out of $sql_myrow["title"] etc etc for starters, which would
> really help on tables with lots of columns.
>
> Then, it'd be great if I could automate this further to automaticaly to
> a stripslashes() on each var, then possibly nl2br() etc.
>
> In psuedo-code I guess it looks something like:
>
> while($sql_myrow = mysql_fetch_array($sql_result))
> {
>
> // we have an array of one row
>
> <use a foreach loop to stripslashes() on each element of the array>
>
> <use a foreach loop to nl2br() on each element of the array>
>
> <use a foreach loop to take each element (eg $sql_myrow["id"]) and
> create a var ($id)>
>
> }
>
> Of course I'm a way newbie on arrays, and I've read the manual, but am
> unclear on the syntax for the above.
>
> Ultimately, I'd like to put this all into a function which I call that
> does it all in one hit, but I'll take that in a smaller step :)
>
> Justin French
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]