使用SQLite PDO从表中返回单个值

时间:2012-10-10 04:41:59

标签: php sqlite pdo

我有一个存储设置及其值的表,从数据库中重新获取值的最佳方法是什么?

设置表格如下:

setting    value
 host       1.2.3.4
 port       1234
 user       me
 pass       cake

例如:

$host = $db->query('SELECT value FROM settings where setting = "host" ');
print $host;

1 个答案:

答案 0 :(得分:3)

我正在使用PDO

$sth = $dbh->prepare('SELECT value FROM settings where setting = :host');
$sth->bindValue(':host', $host, PDO::PARAM_STR);
$sth->execute();
$value = $sth->fetch();
return $value;

这是现在推荐的与数据库作出反应的方法。