PHP PDO从行索引返回列值

时间:2014-07-29 16:49:25

标签: php sql pdo indexing

我写了这段代码:

$statement = $db->prepare("SELECT answer FROM question WHERE question = :question");
$statement->execute(array(':question' => $question));
$row = $statement->fetch();
$answer = $row['answer']; // this gives me a number

现在我想从列中检索该行的值(给定的数字)+1

我该怎么做才能得到它?

1 个答案:

答案 0 :(得分:0)

选择所有字段(SELECT * FROM...)并使用$row[$answer + 1]

$statement = $db->prepare("SELECT * FROM question WHERE question = :question");
$statement->execute(array(':question' => $question));
$row = $statement->fetch();
$answer = $row['answer'];
$other = $row[$answer + 1];