Jump to content

[SOLVED] best way to pull single values from single field table


hellonoko

Recommended Posts

I have a table called settings that has simple one field rows to store the value for that settings:

 

Field:                    value:

 

idea_bg_color        99999

child_bg_color        66666

 

and so on.

 

What is the best way to pull the value from just the one single row/field?

 

Thanks for your time,

ian

Link to comment
Share on other sites

That did not work. And I am not sure I understand it. I am not sure why people post bits of code in the help forum where those of us who need help don't know what we are doing. I used the below instead:

 

include 'dbconnect.php';

//gets the idea background color from DB
$query = "SELECT idea_bg_color FROM settings LIMIT 1";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);

$idea_bg_color = $rows[0];

//gets the child background color from DB
$query = "SELECT child_bg_color FROM settings LIMIT 1";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);

$child_bg_color = $rows[0];

echo $idea_bg_color;
echo "<br>";
echo $child_bg_color;
exit;

Link to comment
Share on other sites

I am not sure why people post bits of code in the help forum where those of us who need help don't know what we are doing

 

To help you. Were not here to write entire solutions.

 

The way you described you question is completely different to the last solution you posted. Do you mean to tell me you have a seperate field for each and every different setting? That is terrible design and means you would need to alter your table each time you need to add a new setting type. Rediculous. Using my method you would simply add a new record.

Link to comment
Share on other sites

Just because I'm in a good mood, I'll even post an example. Firstly, you need a table with 3 fileds.

 

CREATE TABLE settings (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  k VARCHAR(80) NOT NULL,
  v VARCHAR(80) NOT NULL,   
  PRIMARY KEY (id)
);

 

Thats table design never needs to change. Then, you simply need two functions. One to add new setting key/values pairs and one to retrieve them.

 

<?php

  include 'dbconnect.php';

  function addsetting($key,$val) {
    $sql = "INSERT INTO settings (k,v) VALUES ('$key','$val')";
    if (mysql_query($sql)) {
      return true;
    }
    return false;
  }

  function getsetting($key,$default=false) {
    $sql = "SELECT v FROM settings WHERE k = '$key'";
    if ($ result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        return mysql_fetch_field($result,0);
      }
    }
    return $default;
  }

?>

 

Now an example of there usage.

 

<?php

  // this is all you need do to add a new setting type.
  addsetting('idea_bg_color', '99999');
  addsetting('child_bg_color', '66666');

  echo getsetting('idea_bg_color'); // returns 999999
  echo getsetting('child_bg_color'); // return 666666

  // you can even set a default in case no setting is found.
  echo getsetting('foo', 'ffffff'); // returns ffffff

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.