如何根据数组数据获取列数据和设置变量

时间:2015-03-04 22:07:37

标签: php mysql pdo

我无法将以下代码转换为PDO:

//Retrieve settings
$stmt = $db->prepare("SELECT id, name, value
FROM ".$db_table_prefix."configuration");   
$stmt->execute();

while ($stmt->fetch()) {
    $settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
}

好的,我做了一些研究,但我完全失去了,我希望有人可以帮助我。我想我越来越近了。这是我正在尝试的:

//Retrieve settings
$stmt = $db->prepare("SELECT id, name, value
FROM ".$db_table_prefix."configuration");   
$stmt->execute();
/* Bind by column name */
$stmt->bindColumn('id', $id);
$stmt->bindColumn('name', $names);
$stmt->bindColumn('value', $value);
while ($stmt->fetch(PDO::FETCH_BOUND)) {
    $settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
}

//Set Settings
$emailActivation = $settings['activation']['value'];
$mail_templates_dir = "models/mail-templates/";
$websiteName = $settings['website_name']['value'];
$websiteUrl = $settings['website_url']['value'];
$emailAddress = $settings['email']['value'];
$resend_activation_threshold = $settings['resend_activation_threshold']['value'];
$emailDate = date('dmy');
$language = $settings['language']['value'];
$template = $settings['template']['value'];

我要做的是使用DB中的列数据设置变量。所以基本上有一个名称列和一个值列。

1 个答案:

答案 0 :(得分:1)

试试这个:

$stmt->execute();
$result = $stmt->fetchAll();
var_dump($result);
/*
while ($stmt->fetch()) {
    $settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
}
*/