我有一个问题:
$sql = "SELECT e.*, c.name city_name, u.*, IF(l.item IS NULL, 0, 1) as iLike, DATE_FORMAT(e.date, '%h:%i') hour, DATE_FORMAT(e.date, '%d/%c/%Y') day, DATE_FORMAT(e.date, '%c') month
FROM `events` e
LEFT OUTER JOIN `like` l ON l.item = e.uuid AND l.user = :user
LEFT OUTER JOIN city c ON c.city = e.city
LEFT OUTER JOIN user u ON u.user = e.user
WHERE
e.deleted = 0
AND DATE(e.date) >= CURDATE()
AND e.user = :user
ORDER by e.date ASC
";
$select = $eventsModel->getAdapter();
$select->setFetchMode(Zend_Db::FETCH_ASSOC);
$prepare = $select->query($sql, array('user'=>$this->user_id));
如果我硬编码:用户查询,此代码返回结果。例如:
$sql = "SELECT e.*, c.name city_name, u.*, IF(l.item IS NULL, 0, 1) as iLike, DATE_FORMAT(e.date, '%h:%i') hour, DATE_FORMAT(e.date, '%d/%c/%Y') day, DATE_FORMAT(e.date, '%c') month
FROM `events` e
LEFT OUTER JOIN `like` l ON l.item = e.uuid AND l.user = 23
LEFT OUTER JOIN city c ON c.city = e.city
LEFT OUTER JOIN user u ON u.user = e.user
WHERE
e.deleted = 0
AND DATE(e.date) >= CURDATE()
AND e.user = 23
ORDER by e.date ASC
";
$select = $eventsModel->getAdapter();
$select->setFetchMode(Zend_Db::FETCH_ASSOC);
$prepare = $select->query($sql);
这只在我们将代码移动到新服务器时才开始发生, 新的操作系统是Debian 6,squeze,mysql 5.5,PHP 5.3.21。
谢谢,
答案 0 :(得分:1)
使用?
代替:user
,$this->user_id
代替array('user'=>$this->user_id)
此外,您无法在JOIN
语句中绑定数据,因此l.user = e.user
$sql = "SELECT e.*, c.name city_name, u.*, IF(l.item IS NULL, 0, 1) as iLike, DATE_FORMAT(e.date, '%h:%i') hour, DATE_FORMAT(e.date, '%d/%c/%Y') day, DATE_FORMAT(e.date, '%c') month
FROM `events` e
LEFT OUTER JOIN `like` l ON l.item = e.uuid AND l.user = e.user
LEFT OUTER JOIN city c ON c.city = e.city
LEFT OUTER JOIN user u ON u.user = e.user
WHERE
e.deleted = 0
AND DATE(e.date) >= CURDATE()
AND e.user = ?
ORDER by e.date ASC
";
$select = $eventsModel->getAdapter();
$select->setFetchMode(Zend_Db::FETCH_ASSOC);
$prepare = $select->query($sql, array($this->user_id));