0

How do I pass date into placeholder for PHP and MS SQL?

The sample code I am using:

$fromDate = '2013-01-01';
$toDate = '2013-02-28';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ?
           AND toDate >= ? AND fromDate <= ?";

$sqlResult = $this->db->query($sqlStr, $empno, $fromDate, $toDate);

I could not get anything from the database unless I hard code the date values.

Can anyone please help?

Thank you.

2

3 Answers 3

0

Try this ...

$fromDate = '2013-01-01 00:00:00';
$toDate = '2013-02-28 00:00:00';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = $empno
           AND toDate >= '$fromDate' AND fromDate <= '$toDate'";

$sqlResult = $this->db->query($sqlStr);
Sign up to request clarification or add additional context in comments.

1 Comment

Wish I could but I have to use placeholders.
0

Straight off the top of my head, try changing:

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ? AND toDate >= ? AND fromDate <= ?";

to

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ? AND toDate >= '?' AND fromDate <= '?'";

Comments

0

its all in the '' or "" i rather to use "";

$fromDate = '2013-01-01';
$toDate = '2013-02-28';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ?
       AND toDate >= '{$fromDate}' AND fromDate <= '{$toDate}' ";

$sqlResult = $this->db->query($sqlStr, $empno, $fromDate, $toDate);

mind the commas around the vars.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.