问题描述
我有一个可以从绑定变量中受益的简单SQL查询,所以我这样写:
I have simple SQL query that could benefit from bind variables so I have written like this:
$stmt = $this->db->prepare("SELECT * FROM activities WHERE user_id=':user_id' AND date(start_time)=date(':on_specific_day')");
$stmt->bindParam(':user_id',$where['user_id']);
$stmt->bindParam(':on_specific_day',$where['on_specific_day']);
如您所见,有一个名为where
的关联数组,用于存储我的where条件.当我执行此语句时,它不返回任何错误,但行数为零.相反,如果我放弃了使用绑定变量的梦想,那就这样做:
As you can see there is an associative array called where
which is used to store my where conditions. When I execute this statement it doesn't return any errors but the row count is zero. If I instead discard my dream of using bind variables and do this:
$stmt = $this->db->prepare("SELECT * FROM activities WHERE user_id='{$where['user_id']}' AND date(start_time)=date('{$where['on_specific_day']}')");
查询运行得很好,并在我的测试用例中返回2个结果.有人可以帮我摆脱疯狂. :^)
The query runs just fine and returns 2 results in my test case. Can someone help me from slipping into madness. :^)
推荐答案
您无需在PDO parameters
周围加上引号:
You don't need to enclose your PDO parameters
with quote marks:
$stmt = $this->db->prepare("SELECT * FROM activities WHERE user_id=:user_id AND date(start_time)=date(:on_specific_day)
这篇关于PDO的绑定参数行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!