Page 1 of 1

SELECT 'variable for column'?

Posted: Sat Jun 24, 2006 2:11 pm
by WorldCom
Ok, I hope I can explain this good.

The first part is to retreive a specific value from a table. This works fine and returns what I want. In fact, with the constraints, it will only return ONE variable eg $array_variable[0]

Code: Select all

$array_variable = mysql_fetch_array(mysql_query("SELECT this_column FROM table WHERE this_row='$this_row' AND id='$id'"));
This works fine. Now, with $array_variable[0], this represents the NAME of a column in another table and I want to retreive the value from that table.

Code: Select all

$new_variable = mysql_fetch_array(mysql_query("SELECT '$array_variable' FROM new_table WHERE this_row = '$this_row'"));
This does not work.
But it's my example. See where '$array_variable' is the variable retreived from the first query but it needs to identify a column in the second query. If this works, it will retreive only ONE variable again. And thats what I need it to do.

So the big question is, can you use a variable to identify a column in a MySQL QUERY?

I hope I posted this right :)

Posted: Sat Jun 24, 2006 2:19 pm
by Weirdan
use backticks (`) instead of single quotes(') around column name in your second query

Posted: Sat Jun 24, 2006 3:55 pm
by WorldCom
That worked ....... thanks so much :)