variable inside a $row statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
russ72g
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 2:55 am

variable inside a $row statement

Post by russ72g »

hi,

i am really struggling to find a way around this one but i am sure it's something simple for those who know.

i am working on a page where my user will input two languanges from a form: a mother language and a target language. these are stored under $mothlang and $targlang. i have a db table with words stored in many languages, with field names english, spanish, german etc. etc.

to get an english word from a row i would just use this code:
echo $row['english'];

to get the spanish:
echo $row['spanlish']; etc.

but what i want is to use the variable to control what is retrieved from the table. so something like this code:

echo $row['$mothlang'];

this code doesn't work however. obviously there is a correct way to do this but i can't find the answer anywhere. any help greatly appreciated.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: variable inside a $row statement

Post by VladSun »

Code: Select all

echo $row[$mothlang];
Try

Code: Select all

$s = "String";
echo 'This is a $s';
echo "This is a $s";
echo "This is a ".$s;
echo 'This is a '.$s;
to see the differences in using single/double quotes and string concatenation.
There are 10 types of people in this world, those who understand binary and those who don't
russ72g
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 2:55 am

Re: variable inside a $row statement

Post by russ72g »

I knew it would be something simple. Many thanks for you your time. That has solved the problem and I have learned something. :)
Post Reply