SQL查询中的解析错误定义为PHP类属性

时间:2015-09-27 05:54:36

标签: php sql oop properties double-quotes

当我将下面的SQL查询定义为PHP类属性时,我得到一个解析错误。如果我在其中一个类方法中定义相同的查询,我没有得到任何错误。有人可以告诉我如何正确地将其定义为PHP类属性?谢谢。

我将其他SQL查询定义为可以正常工作的属性,但它们不包含任何变量。

private $querySelectDateId = " SELECT healthDateId FROM kidsDate WHERE healthDate = '$healthDate' ";

2 个答案:

答案 0 :(得分:1)

您不能在属性声明中使用变量,尤其是非本地上下文中的本地变量(同样$this->healthDate也不起作用)。一种方法是在__construct()

中声明属性值
public function __construct()
{
    $healthDate = '2015-09-27';
    $this->querySelectDateId = " SELECT healthDateId FROM kidsDate WHERE healthDate = '$healthDate' ";
}

答案 1 :(得分:0)

private $querySelectDateId;

public function setSelectDateId($healthDate) {
    $this->querySelectDateId = " SELECT healthDateId FROM kidsDate WHERE healthDate = '$healthDate' ";
}

public function getSelectDateId() {
    return $this->querySelectDateId ;
}