Sam
Sam

Reputation: 7078

Variables in an array

I'm trying to put a mysql query into a function, however when I do a mysql_fetch_array I need to specify the row's name. How can I put a variable in a function?

function config($setting_name,){
    while($config_db = mysql_fetch_array(mysql_query("SELECT {$setting_name} FROM config WHERE id='1'") {
        echo $config_db['@@@@HERE@@@@'];
    }
}

Upvotes: 0

Views: 130

Answers (2)

shamittomar
shamittomar

Reputation: 46712

You can use the mysql AS keyword and get an alias for it. Like this:

    // Using MyOutput in query:
    function config($setting_name,){
        while($config_db = mysql_fetch_array(mysql_query("SELECT {$setting_name} AS Myoutput FROM config WHERE id='1'") {
            echo $config_db['Myoutput'];
        }
    }

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

while($config_db = mysql_fetch_array(mysql_query("SELECT $setting_name FROM config WHERE id='1'") {
    echo $config_db[$setting_name];
}

Be sure not to pass user input directly into this function. Also, I don't think you need the curly braces, because it's not a complex expression.

Upvotes: 1

Related Questions