Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP/MySQL Making the value of one column the key for another

Contributor ,
Apr 03, 2011 Apr 03, 2011

Copy link to clipboard

Copied

I have a table 'languages' with columns field, english, french, german.
I have variable $language = 'french'

I want the value of 'field' to become the key for the value of 'french'
For example, if field = city and french = ville, I want to end up with $city having the value ville.

I have tried many ways, including the following, but nothing works.

$sql=mysql_query("SELECT field, $language FROM languages");
while($set=mysql_fetch_assoc($sql)){
foreach ($set as $value){
$now = array($set['field'] => $set[$language]);
}
extract($now);
echo $city;

TOPICS
Server side applications

Views

584
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 03, 2011 Apr 03, 2011

Copy link to clipboard

Copied

LATEST

Someone at the DevNetwork forum gave the answer for this.

In case anyone is interested, the solution is to use variable variables, and the code is revised like so:

$sql=mysql_query("SELECT field, $language FROM languages");
while($set=mysql_fetch_assoc($sql)){
${$set['field']} = $set[$language];
}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines