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.

Jamie
  • 433
  • 7
  • 15

3 Answers3

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);
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
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 <= '?'";
Raad
  • 4,540
  • 2
  • 24
  • 41
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.

Meta_data
  • 558
  • 3
  • 14