0

I have this code which i am testing from last couple of hour but unable to understand that why TodayAttendanceCount is not returning 0.

    $sqlCheckLoginEntryCount = "SELECT Count(*) TodayAttendanceCount FROM AttendanceMachineLogin
                                WHERE Date(RecordAddDateTime) = :RecordAddDateTime
                                AND TimeTrackId = :TimeTrackId";

   $statementEntryCount = $connPDO->prepare($sqlCheckLoginEntryCount);

   $queryParams = array(
        ':TimeTrackId'          =>    $TimeTrackId,
        ':RecordAddDateTime'    =>    $RecordAddDateTime
    );

   $statementEntryCount->execute($queryParams);

   $queryData = $statementEntryCount->fetch();

   echo '\n ';
   //var_dump($queryData);
   echo "\n Attendance Count". $queryData['TodayAttendanceCount'] ." ;";

I have executed the same query in MySqlWorkbench which is working fine and there is data and it is fine from the database side.

Ahmed Numaan
  • 1,034
  • 1
  • 10
  • 26
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

2 Answers2

0

Remove : part at the $queryParams. It isn't needed. The code will look like this.

$queryParams = array(
    'TimeTrackId'          =>    $TimeTrackId,
    'RecordAddDateTime'    =>    $RecordAddDateTime
);
david
  • 3,225
  • 9
  • 30
  • 43
-1

Try this:

    $sqlCheckLoginEntryCount = "SELECT Count(*) As TodayAttendanceCount FROM AttendanceMachineLogin
                                WHERE Date(RecordAddDateTime) = ':RecordAddDateTime'
                                AND TimeTrackId = :TimeTrackId";

   $statementEntryCount = $connPDO->prepare($sqlCheckLoginEntryCount);

   $queryParams = array(
        'TimeTrackId'          =>    $TimeTrackId,
        'RecordAddDateTime'    =>    $RecordAddDateTime
    );

   $statementEntryCount->execute($queryParams);

   $queryData = $statementEntryCount->fetch(PDO::FETCH_ASSOC);

   echo '\n ';
   //var_dump($queryData);
   echo "\n Attendance Count ". $queryData['TodayAttendanceCount'];

Hope I pushed you further.

ThS
  • 4,597
  • 2
  • 15
  • 27
  • An explanation of what you've changed and how this should solve the problem would help. – Nigel Ren Aug 15 '18 at 07:21
  • @NigelRen the `SQL` query misses the `As` keyword. The array of the desired values shouldn't have preceding colons in the array keys. – ThS Aug 15 '18 at 07:26
  • `as` is optional in MySQL, also check out https://stackoverflow.com/questions/9778887/is-the-leading-colon-for-parameter-names-passed-to-pdostatementbindparam-opt – Nigel Ren Aug 15 '18 at 07:29
  • but we shouldn't rely on this behaviour and in the official documentation the colons are ommited. – ThS Aug 15 '18 at 07:34
  • I'm in favour of not using undocumented features, but there is a difference between something that happens to work (but shouldn't) and something that doesn't work at all. As such although it's good to change it - will it solve OP's problem? – Nigel Ren Aug 15 '18 at 07:39
  • good point, but, that's why we have "best practices" in the programming/programmers industry. So, we should, only, rely on official documentations. The OP may or may not be helped out with that but these points "will" help another person, let me say: if you know something good share it ! – ThS Aug 15 '18 at 07:48