I have a query that uses a php variable, and I am having a bit of trouble fetching the result. I'll create a textbook example.
Example:
$variable=the name of a column ie name, address, etc.
$query="select '$variable', count(*) from table, sort by '$variable' asc";
$r=mysql_query($query) or die(mysql_error());
while($w=mysql_fetch_array($r, MYSQL_ASSOC))
{$num=$w["count(*)"];
Ok, that line is just fine. It returns the correct number of instances of the $variable in the table with each iteration. The problem comes when I try to retrieve the value of the given $variable column.
$val=$w[$variable];
that one just retrieves the column name. It just throws out, for example, "name" each time through the loop.
$val=$w["$variable"];
Does the same thing.
How can I make that statement work?
Example:
$variable=the name of a column ie name, address, etc.
$query="select '$variable', count(*) from table, sort by '$variable' asc";
$r=mysql_query($query) or die(mysql_error());
while($w=mysql_fetch_array($r, MYSQL_ASSOC))
{$num=$w["count(*)"];
Ok, that line is just fine. It returns the correct number of instances of the $variable in the table with each iteration. The problem comes when I try to retrieve the value of the given $variable column.
$val=$w[$variable];
that one just retrieves the column name. It just throws out, for example, "name" each time through the loop.
$val=$w["$variable"];
Does the same thing.
How can I make that statement work?