SELECT 'variable for column'?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

SELECT 'variable for column'?

Post 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 :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

use backticks (`) instead of single quotes(') around column name in your second query
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Post by WorldCom »

That worked ....... thanks so much :)
Post Reply