0

I am trying to use a variable to refer to a column of a fetch_array.
Problem is it is not working:

$table_field = "name";
$result = "select * from table";
while($myrow = mysql_fetch_array($result);
 $text .= $myrow[$table_field] . ", "; // Note: if I use 'name' it works fine...
}
echo $text;

Can anyone see a problem with my code or something I am missing?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by OmniX
1

You have a broken loop statement!!
Try the following:

$table_field = 'name';
$result = "select * from table";
while($myrow = mysql_fetch_array($result)) {
 $text .= $myrow[$table_field] . ", "; // Note: if I use 'name' it works fine...
}
echo $text;
Votes + Comments
Thanks for the help.
0

Ya that was an error from me coming up with a quick similar example.
I noticed in your code also:

$table_field = 'name';
 // where mine was
 $table_field = "name";

Thanks for the help, much appreciated.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.