我正在尝试创建一个查询,从我的数据库中提取有关卖家的信息,但前提是他们的商店在过去 3 天内推出。我能想到的计算查询日期的最简单方法是使用new DateTime()
对象。当我输出我的代码来测试它时,它位于 MySQL 用来查询它的正确字符串中,但是每当我尝试绑定变量时,我都会收到错误消息。我正在使用 Zend_Db 进行查询,(PDO 适配器)
行动:
public function indexAction()
{
$dateMod = new DateTime();
$dateMod->modify('-2 days');
$dateMod->format('Y-m-d H:i:s');
// get sellers initialized in last 3 days
$sellerTable = new Application_Model_DbTable_Sellers();
$select = $sellerTable->select()->setIntegrityCheck(false);
$select->from(array('s' => 'seller'),array('sellerID', 'businessName'));
// select firstName, lastName, picture from user table, and businessName and sellerID from seller table.
$select->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName', 'picture'));
$select->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured = 1');
$select->where('s.launchDate > ?', $dateMod);
$select->order('s.launchDate DESC');
$newSellers = $sellerTable->fetchAll($select);
当我分配$dateMod
给视图时,它会输出正确的Y-m-d H:i:s
格式。但是当我将它插入查询时,我收到以下错误:
Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY `b`.`launchDate` DESC' at line 2
如果我以 mysql 时间戳格式将值硬编码到 dateMod 中,则查询工作正常。如何仅访问 DateTime 对象中时间戳的字符串值? getTimestamp
返回 unix 格式的时间戳,即使在分配格式之后也是如此。