0

When I define the SQL query below as a PHP class property I get a parse error. If I instead define that same query inside one of the class methods, I get no error. Can someone please tell me how I can define this as a PHP class property correctly? Thank you.

I have other SQL queries defined as properties that work fine, but they do not contain any variables.

private $querySelectDateId = " SELECT healthDateId FROM kidsDate WHERE healthDate = '$healthDate' ";
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • What is the _parse error_ you're getting? – DirtyBit Sep 27 '15 at 06:41
  • `Parse error: syntax error, unexpected '"' in /usr/local/apache2/htdocs/marshall/kids_health/includes/health_DateWork.php.inc on line 8` Line 8 is the line above. It obviously says there is an unexpected double quotes, but i've tried this with as many different sequences as i can think of. With double quotes on the outside, single quotes on the outside, double for the variables and so forth. – Kevin Marshall Sep 27 '15 at 11:23
  • what is on line 8? Kindly post it here – DirtyBit Sep 27 '15 at 11:31

2 Answers2

1

You can't use a variable in a property declaration, especially a local one in a non local context (also $this->healthDate would not work either). One way could be to declare property value in your __construct():

public function __construct()
{
    $healthDate = '2015-09-27';
    $this->querySelectDateId = " SELECT healthDateId FROM kidsDate WHERE healthDate = '$healthDate' ";
}
fpietka
  • 1,027
  • 1
  • 10
  • 23
0
private $querySelectDateId;

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

public function getSelectDateId() {
    return $this->querySelectDateId ;
}
Julio Soares
  • 1,200
  • 8
  • 11
  • While this will work, you really **MUST** use parameters for queries,or you are inviting all kinds of pain: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Smack Jack Sep 27 '15 at 06:07
  • 1
    ^Apart from that, you should however also write _a bit_ about how/why this answer should help. – DirtyBit Sep 27 '15 at 06:08
  • FYI: I am not using prepared statements because this is a tiny one user (me) app that sits on my home local net. Just not that worried about little "Bobby" getting into my home network. – Kevin Marshall Sep 27 '15 at 06:21
  • Can anyone tell my why the code given by Julio Soares would work? Do i really have to replace one line of code with five? – Kevin Marshall Sep 27 '15 at 11:27