0

I have a table called global_settings with 2 columns (field, value)

i need a way to make each row a variable

so for example:

is field = title and value = hello i would need:

$title = 'hello';

I have tried doing this:

$global_sql="SELECT * from global_settings ";
$global_rs=mysql_query($global_sql,$conn) or die(mysql_error());
while($global_result=mysql_fetch_array($global_rs))
{
    $global_sql2="SELECT * from global_settings where field =  '".$global_result["field"]."' ";
    $global_rs2=mysql_query($global_sql2,$conn) or die(mysql_error());
    $global_result2=mysql_fetch_array($global_rs2);

    $global_result2["field"] = $global_result2["value"];
}

but that didn't work - any ideas?

7
  • This is easy to do as an array - $global_settings[$field] = $value. Do you really need to make them specific variables? Commented Jul 26, 2013 at 19:44
  • @andrewsi do i put this outside the while loop or inside Commented Jul 26, 2013 at 19:44
  • i need to be able to echo the variables- for example a website title and email address but more may be added by the user Commented Jul 26, 2013 at 19:46
  • In your code, you could use $config[$global_result2["field"]] = $global_result2["value"]; - just replace the last line of your loop with that. You should also initialise the value of $config before the loop, too. Commented Jul 26, 2013 at 19:46
  • 2
    mysql_* functions are deprecated and shouldn't be used anymore. Use mysqli or PDO instead. Why doing a second query? You already have both the fields and the values. Commented Jul 26, 2013 at 19:48

2 Answers 2

4

I strongly suggest you do not auto-create variables from your DB. You'll be littering your script's namespace with potentially useless variables, and probably overwriting other variables you DID want to keep separate. Do it as you are - store the settings in an array.

Plus, unless I'm totally misreading your code, there's absolutely no reason for the sub-query. A simple:

$settings = array();
$sql = "SELECT field, value FROM settings"
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   $settings[$row['field']] = $row['value'];
}

is all you need to grab all of the settings.

Sign up to request clarification or add additional context in comments.

3 Comments

oops i wrote my answer while you wrote yours, sorry :)
how do i echo just one row?
echo $settings['foo']?
0

This line will not create a variable named after the value for field.

$global_result2["field"] = $global_result2["value"];

You can create an array variable, for example:

$array_of_results[ $global_result2["field"] ] = $global_result2["value"];

This way you will have the following array:

[ 'field' => 'value' ] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.