hellonoko Posted December 27, 2007 Share Posted December 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
trq Posted December 27, 2007 Share Posted December 27, 2007 SELECT `value` FROM tbl WHERE `Field` = 'idea_bg_color'; Quote Link to comment Share on other sites More sharing options...
hellonoko Posted December 27, 2007 Author Share Posted December 27, 2007 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; Quote Link to comment Share on other sites More sharing options...
trq Posted December 27, 2007 Share Posted December 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted December 27, 2007 Share Posted December 27, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.