Search code examples
phpmysqlrowcell

Getting data from cell


I have a database table called "settings", and in that table I've got 2 columns. Example picture:

enter image description here

As you can see, the first columns is called "setting_name" and the second is called "setting_value".

How can I do, so whenever I write example:

echo $setting['ad_cost_micro'];

It will print out:

0.00200

Btw. is it possible to do without a while statement?


Solution

  • I tend to use PDO, and fetchAll().

    $stmt = $db->prepare('SELECT `setting_name`, `setting_value` FROM `settings`');
    $stmt->execute();
    $setting = $stmt->fetchAll();
    echo $settings['ad_cost_micro']; //Should output 0.00200.
    

    Note: Regardless of what you use, a while loop is unavoidable because you HAVE to loop through the result set (fetchAll()) does that too, also, know that every loop is a while loop (even for loops!).