March 22, 2012

Display a value of an array

Question by user1274113

I’m trying to get a specific value from a mysql_fletch_assoc array. I explain. Here is the database. Table: config with 2 fields: setting and value.

SETTING | VALUE
width | 100    
height | 50

And this is the query:

$result = (mysql_query("SELECT setting, value FROM config"));
while($setting = mysql_Fetch_assoc($result)){


    echo $setting['value'];
    echo "<br>";

Now i can see the entire array. I want to display every element individually. I tryed with $setting[‘value’][‘0’], $setting[‘value’][‘width’] with no success.

Edit: basically i want to do something like this:

echo $setting['value']['width'] gives -> 100 
echo $setting['value']['height'] gives -> 50

I hope that now it’s more clear

Answer by Starx

First, may be there is a typo error mysql_Fetch_assoc, fix this to mysql_fetch_assoc.

And In order to receive the values on your pattern, do the following

$setting = array();
while($row = mysql_fetch_assoc($result)) {
     $setting['value'][$row['setting']] = $row['value'];
}

Now, you can implement them as you want

echo $setting['value']['width']; // will give -> 100 
echo $setting['value']['height']; // will give -> 50

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!