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?

Recommended Answers

All 2 Replies

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;
commented: Thanks for the help. +2

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.