我不明白我的功能做错了什么?
我正在此页面http://php.net/manual/en/pdostatement.execute.php上尝试示例 #2,但告诉我参数编号无效。但我不明白为什么它有效?
if (!isset($_SESSION)) session_start();
$sql = "SELECT challenge FROM challenges WHERE sessionid=':sessionid' AND timestamp > :timestamp LIMIT 1;";
$params = array(':sessionid'=>session_id(), ':timestamp'=>time());
function pdoRS($sql, $params) {
try {
// Prepare Query
$stmt = $dbConn->prepare($sql);
$stmt->execute(($params!=""?$params:NULL));
$result = $stmt->fetchAll();
return $result;
}
catch (PDOException $e) {
// Output error
echo 'Execution Exception: '.$e->getMessage());
return;
}
}
print_r(pdoRS($sql,params));
即使我将其更改为:
$sql = "SELECT challenge FROM im_accounts_challenges WHERE sessionid='?' AND timestamp > ? AND isauthenticated=1 LIMIT 1;";
$params = array(session_id(), time());
我仍然收到相同的消息:
Execution Exception: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens