使用fetch_assoc获取多行对我来说不起作用

时间:2014-11-16 20:22:29

标签: php mysql mysqli

我尝试使用此代码获取多行,但它不返回任何内容。

$settings_table = $db->query("SELECT * FROM `settings`");
while ($row = $settings_table->fetch_assoc()) {
    echo $row["website_name"];
    echo $row["website_tags"];
}

Apatche:2.4,PHP:5.5,MySQL:5.6

1 个答案:

答案 0 :(得分:1)

如果有人有兴趣:

$settings = array();
foreach ($db->query("SELECT setting_value FROM settings") as $row) {
    array_push($settings, $row['setting_value']);
}
echo $settings[0]; // Returns "Cool Flowers"

或按键获取价值:

$settings = array(array(), array());
foreach ($db->query("SELECT setting_key, setting_value FROM settings") as $row) {
    array_push($settings[0], $row['setting_key']);
    array_push($settings[1], $row['setting_value']);
}
function get_value($key) { global $settings;
    $key_num = array_search($key, $settings[0]);
    return $settings[1][$key_num];.
}
echo get_value("ws_tags"); // Returns "Flowers"

数据库:

setting_key:setting_value
ws_name:Cool Flowers
ws_tags:Flowers