我对 pdo 的这种意外行为感到困惑:
考虑我写的这个简单的查询:
$username = "vidhu";
$numResults = 10;
$db_vc = new PDO(DB_ADDRESS, DB_USER, DB_PASS);
$stmt = $db_vc->prepare("SELECT username, email FROM users WHERE username = :username LIMIT :numResults");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':numResults', $numResults, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
这给了我预期的输出:
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com) )
现在让我感到困惑的是。当我像这样复制并粘贴查询时
$username = "vidhu";
$numResults = 10;
$db_vc = new PDO(DB_ADDRESS, DB_USER, DB_PASS);
$stmt = $db_vc->prepare("SELECT username, email FROM users WHERE username = :username LIMIT :numResults");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':numResults', $numResults, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
echo "<br />";
$username = "vidhu";
$numResults = 10;
$db_vc = new PDO(DB_ADDRESS, DB_USER, DB_PASS);
$stmt = $db_vc->prepare("SELECT username, email FROM users WHERE username = :username LIMIT :numResults");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':numResults', $numResults, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
我希望输出也会被复制,对吗?就像:
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com ) )
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com) )
但它不会产生那个输出!第二个查询不返回它显示的任何内容,如下所示:
Array ( [0] => Array ( [username] => vidhu [email] => xx.vidhuxx@gmail.com ) )
Array ( )
为什么是这样?有人可以解释吗?
编辑
如果我删除:numResults
原始和副本中的参数以及查询中的硬编码 10,它会完美运行!