Jump to content

[SOLVED] Selecting Data from Database(php/mysql).


Demonic

Recommended Posts

Alright I've come to a point where I only want a DB for settings and nothing based on ID's on a sql table.

 

instead of say:

 

$setting = mysql_query("SELECT * FROM settings WHERE id='1' ");

 

Now i broken up all the sql into seperate settings like so:

 

settingname | value

 

boardonline | no

 

Thats basicaly how I got the sql set up instead of having it all in one table row I have them in seperate rows.

 

So how do I go about selecting data from SQL not based on ID but mainly on setting name?

 

would I do

 

$settings = mysql_query("SELECT * FROM settings");

$settings = mysql_fetch_array($settings);

$settings['boardonline'][1];//would echo true/false according to my sql table instead of the row name?

 

Thanks in advance for helping.

Link to comment
Share on other sites

Sorry, Im not being real helpfull. I see what your doing now and I've done it before, something like this will be much easier.

 

<?php

  function getsettings($settingsstring) {
    // connect to db.
    if ($result = mysql_query("
      SELECT settingname, value
      FROM settings
      WHERE settingname
      IN($settingsstring);")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
          $return[$row['settingname'] => $row['value']];
        }
      }
    }
    return $return;
  }

  $settings = getsettings("bordonline,foo,bar,bip");

?>

 

This will return an array of keys/values. Much easier to work with.

Link to comment
Share on other sites

an update is much simpler because you can only update one record at a time. However, you could create an array to do so. eg;

 

<?php

  function updatesettings($arr) {
    // connect to db.
    foreach ($arr as $k => $v) {
      if (!mysql_query("UPDATE settings SET value = '$v' WHERE settingname = '$k'")) {
        return false;
      }
    }
    return true;
  }

  if (updatesettings(array('bordonline' => 'no','foo' => 'bar')) {
    echo "update success";
  }

?>

Link to comment
Share on other sites

Im getting a wierd error:

Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/nevux/public_html/fo****/*******/sources/wordfilter.php on line 188

  function getsettings($settingsstring) {
    if ($result = mysql_query("
      SELECT `setting`, `value`
      FROM `wf_settings`
      WHERE `setting`
      IN($settingsstring);")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
          $return[$row['setting'] => $row['value']];//IS LINE 188
        }
      }
    }
    return $return;
  }

Link to comment
Share on other sites

Must be a problem then because when I test it it doesn't work:

 

sql:

CREATE TABLE `wf_settings` (
  `setting` varchar(255) NOT NULL default '',
  `value` varchar(255) NOT NULL default 'yes'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- 
-- Dumping data for table `wf_settings`
-- 

INSERT INTO `wf_settings` VALUES ('topic_title', 'yes');
INSERT INTO `wf_settings` VALUES ('topic_posts', 'yes');
INSERT INTO `wf_settings` VALUES ('signature', 'yes');
INSERT INTO `wf_settings` VALUES ('pm_subject', 'yes');
INSERT INTO `wf_settings` VALUES ('pm_body', 'yes');
INSERT INTO `wf_settings` VALUES ('customfields', 'yes');
INSERT INTO `wf_settings` VALUES ('username', 'no');
INSERT INTO `wf_settings` VALUES ('globalall', 'no');

 

 

  function getsettings($settingsstring) {
//already connecting to DB
    if ($result = mysql_query("
      SELECT `setting`, `value`
      FROM `wf_settings`
      WHERE `setting`
      IN($settingsstring);")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          $return(array($row['setting'] => $row['value']));
        }
      }
    }
    return $return;
  }
$set = getsettings("topic_title,topic_posts,signature,pm_subject,pm_body,customfields,username,globalall");
echo $set[topic_title];

Link to comment
Share on other sites

Ok... I see the problem. Need to make a few adjustments. The function will need to accept an array now. Try...

 

<?php

  function getsettings($settingsarray) {
    if ($result = mysql_query("
      SELECT `setting`, `value`
      FROM `wf_settings`
      WHERE `setting`
      IN('".implode("','",$settingsarray)."');")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          $return(array($row['setting'] => $row['value']));
        }
      }
    }
    return $return;
  }

?>

 

And call it using....

 

<?php

  $set = getsettings(array('topic_title','topic_posts','signature','pm_subject','pm_body','customfields','username','globalall'));

?>

Link to comment
Share on other sites

Is that error when calling the array using the new method I suggested? Barands method would have meant you could leave the function as it was earlier, I thought the newer way of calling the function seemed more logical.

Link to comment
Share on other sites

when calling your new way I tried it then I got undeclared function on the $return array.

 

So I changed it to the following:

 

  function getsettings($settingsarray) {
    if ($result = mysql_query("
      SELECT `setting`, `value`
      FROM `wf_settings`
      WHERE `setting`
      IN('".implode("','",$settingsarray)."');")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          $return[array($row['setting'] => $row['value'])];
        }
      }
    }else{
	$return = mysql_error();
}
    return $return;
  }

 

Then I tried this:

 

	$set = getsettings(array('topic_title','topic_posts','signature','pm_subject','pm_body','customfields','username','globalall'));
	print_r($set);

 

Now im not getting any errors nor any results.

Link to comment
Share on other sites

Humm I made my own and it works the same I guess: btw thorpe yours didn't work.

 

function WFSet($WFset = array()){

	foreach($WFset as $WFsett){

	$ro = mysql_query("SELECT * FROM `wf_settings` WHERE `setting` = '$WFsett' ") or die(mysql_error());
	$row = mysql_fetch_array($ro);

	$result[$WFsett] = array($row[setting] => $row[value]);

	}

return $result;

}

 

Result:

 

Array
(
    [topic_title] => Array
        (
            [topic_title] => yes
        )

    [topic_posts] => Array
        (
            [topic_posts] => yes
        )

    [signature] => Array
        (
            [signature] => yes
        )

    [pm_subject] => Array
        (
            [pm_subject] => yes
        )

    [pm_body] => Array
        (
            [pm_body] => yes
        )

    [customfields] => Array
        (
            [customfields] => yes
        )

    [username] => Array
        (
            [username] => no
        )

    [globalall] => Array
        (
            [globalall] => no
        )

)

Link to comment
Share on other sites

Thats what I get for not testing code before posting. Theres just a slight syntax error in my code, should be....

 

<?php

  function getsettings($settingsarray) {
    if ($result = mysql_query("
      SELECT setting, value
      FROM wf_settings
      WHERE setting
      IN('".implode("','",$settingsarray)."');")) {
      if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_array($result)) {
          $return[$row['setting']] = $row['value'];
        }
      }
    }
    return $return;
  }

?>

 

That will return a nice neat associative array.

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.