I am perplexed by this unexpected behavior from pdo:
我对pdo的这种意外行为感到困惑:
Consider this simple query i have written:
考虑一下我写的这个简单的查询:
$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);
This gives me the expected output:
这给了我预期的输出:
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com) )
Now here is what baffles me. When i copy and paste the query EXACTLY like so
现在这就是让我感到困惑的地方。当我像这样复制并粘贴查询时
$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);
I expect the output also to be duplicated right? so like:
我希望输出也是重复的吗?所以喜欢:
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com ) )
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com) )
but it doesnt produce that output!. The seond query doesnt return anything it shows up like this:
但它不会产生那个输出! seond查询不会返回它显示的任何内容,如下所示:
Array ( [0] => Array ( [username] => vidhu [email] => xx.vidhuxx@gmail.com ) )
Array ( )
Why is this? Can someone explain?
为什么是这样?谁能解释一下?
Edit
编辑
If i remove the :numResults
parameter in both the original and the copy and hardcode 10 in the query it works perfectly!
如果我删除原始和副本中的:numResults参数和查询中的硬编码10它完美地工作!
1 个解决方案
#1
0
Ok So i think i found out the problem.
好吧我觉得我发现了问题所在。
between the first copy and second copy i added var_dump($numResults)
. When the page is loaded is shows that the variable has been converted to a string after the first copy of the query is executed. I really don't know why that happens.
在第一个副本和第二个副本之间我添加了var_dump($ numResults)。加载页面时显示在执行查询的第一个副本后变量已转换为字符串。我真的不知道为什么会这样。
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com ) )
string(2) "10"
Array ( )
So the way i fixed it was instead of using $stmt->bindParam
i used $stmt->bindValue
Again, i don't know the mechanics or what is going on but it works.
所以我修复它的方式不是使用$ stmt-> bindParam我使用$ stmt-> bindValue再次,我不知道机制或发生了什么,但它的工作原理。
It would be nice if someone could explain why this type of behavior occurs i.e Why the variable is changed from a integer to a string.
如果有人可以解释为什么会发生这种行为,那么为什么变量从整数变为字符串会更好。
#1
0
Ok So i think i found out the problem.
好吧我觉得我发现了问题所在。
between the first copy and second copy i added var_dump($numResults)
. When the page is loaded is shows that the variable has been converted to a string after the first copy of the query is executed. I really don't know why that happens.
在第一个副本和第二个副本之间我添加了var_dump($ numResults)。加载页面时显示在执行查询的第一个副本后变量已转换为字符串。我真的不知道为什么会这样。
Array ( [0] => Array ( [username] => vidhu [email] => someone@gmail.com ) )
string(2) "10"
Array ( )
So the way i fixed it was instead of using $stmt->bindParam
i used $stmt->bindValue
Again, i don't know the mechanics or what is going on but it works.
所以我修复它的方式不是使用$ stmt-> bindParam我使用$ stmt-> bindValue再次,我不知道机制或发生了什么,但它的工作原理。
It would be nice if someone could explain why this type of behavior occurs i.e Why the variable is changed from a integer to a string.
如果有人可以解释为什么会发生这种行为,那么为什么变量从整数变为字符串会更好。