PDO的绑定参数行为异常

编程入门 行业动态 更新时间:2024-10-18 03:34:34
本文介绍了PDO的绑定参数行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个可以从绑定变量中受益的简单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的绑定参数行为异常

本文发布于:2023-11-16 11:37:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   异常   参数   PDO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!