Need quick help getting values from mysql

Discussion in 'MySQL' started by oseer, Sep 27, 2011.

  1. #1
    I have a table called "system_config". The structure is as follows:

    id | config_name | config_value

    So, an entry might be:

    1 | language | english

    I am having trouble getting these values out of the DB in a particular way. I am trying to get where I can do something like:

    echo $config["language"];
    PHP:
    and see the word "english" on the page.

    Any suggestions???
     
    oseer, Sep 27, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Have you tried hiring someone to do this for you? If not, what have you got so far? Where are you stuck?
     
    plog, Sep 27, 2011 IP
  3. oseer

    oseer Member

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Well I have this:

    
    $systemconfig = mysql_query("SELECT * FROM system_config");    
    
    while ($configs = mysql_fetch_array($systemconfig)) {
        
    echo $configs["config_name"].": ".$configs["config_value"]."<br />";            
    
    }
    
    PHP:
    ...But this forces me to just echo all the values in the while loop. I need to be able to place these values at different places throughout the page.

    What I'm thinking now is that I need to use class objects somehow so that I can do something like:

    echo $config->language;
    PHP:
    ...to echo the value "English" on the page.

    I really need some help trying to get this going.
     
    oseer, Sep 28, 2011 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    How do you know which row to use and when? Presumably $configs['config_name'] will contain different values and not just "English" depending on what row you are working with. Also, in your first post you wanted an array ($config['language']) and now you've stated you want an object ($config->language).

    Can you provide more clarification on exactly what you are trying to output to the browser?
     
    plog, Sep 28, 2011 IP
  5. oseer

    oseer Member

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    I'm trying to figure which method would work best. Because $configs['config_name'] will contain different values, that's why I now believe an object would be better.

    The only thing I'm trying to output to the browser is the config_value.

    That's why I think I can do something like $config->language to get "English", or $config->country to get "US", etc. I am just lost when working with objects.
     
    oseer, Sep 28, 2011 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Ok, this will work like you initially posted, however, if you have 2 records in your database that have the same config_name (i.e. 'language', the last one pulled will be the value you get.)

    
    $config=array();
        // array to hold all config_name values 
    
    $systemconfig = mysql_query("SELECT * FROM system_config;");    
    while ($configs = mysql_fetch_array($systemconfig)) $config[$configs['config_name']]=$configs['config_value'];
        // puts config values into $config where config_name is array key
    
    echo $config['language'];
    
    
    PHP:
     
    plog, Sep 28, 2011 IP
  7. oseer

    oseer Member

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Thank you for your help, but like I mentioned, this would only work in the while loop. That's pretty much identical to the code I have. I think it's going to have to be done as an object from everything I can tell (but I could be wrong)
     
    oseer, Sep 28, 2011 IP
  8. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #8
    I don't understand what you mean by "would only work in the while loop". Since your extracting an unknown amount of rows from a query, you are going to have to use a while loop no matter what. All the data in the $config array will be accessible outside of the while loop.
     
    plog, Sep 28, 2011 IP