本文介绍了PDO和MySQL之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让PDO与介于两者之间"的MySQL一起使用.下面是我的代码:
I'm trying to get PDO to work with a MySQL 'between'. Below is my code:
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$start_date = date('Y-m-d H:i:s', mktime(0, 0, 0, 11, 1, 2009));
$end_date = date('Y-m-d H:i:s', mktime(23, 59, 59, 11, 30, 2009));
$STH = $DBH->prepare("SELECT * FROM `table` WHERE `start_date` BETWEEN ':start_date' AND ':end_date'");
$STH->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$STH->bindParam(':end_date', $end_date, PDO::PARAM_STR);
$STH->execute();
var_dump($row);
返回的是一个值为'0'或'NULL'的数组.当我对结束日期进行硬编码时,它就像将start_date
设置为-1一样,重新调整了end_date
之前的所有行.那么,我在这里做错了什么?
What gets returned is an array with '0' or 'NULL' for values. When I hard code the end date, it acts as if start_date
is set to -1, retuning me all rows before the end_date
. So, what am I doing wrong here?
推荐答案
不要用单引号引起来的值.
Don't wrap the values with single quote.
$STH = $DBH->prepare("SELECT * FROM `table` WHERE `start_date` BETWEEN :start_date AND :end_date");
这篇关于PDO和MySQL之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!