PDO声明可能不正确

时间:2013-01-13 02:22:07

标签: php mysql pdo

我有下面的PDO语句,我试图找到是否有与该语句匹配的行,如果为true则返回1,然后回显“有匹配”。由于某种原因,即使我的数据库在第24行显然有匹配,它也会继续返回false。我希望有人能为我发现错误。

PDO声明:

<?php

$employeeID = 1;
$dateToday = "01/12/13";

try{
    $conn = new PDO('mysql:host=localhost;dbname=timecard', 'username', 'password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT COUNT(*) FROM timeRecords WHERE `timeOut`= 'NULL' AND `employeeID`= :employeeID AND `date`= :dateToday");

    $stmt->execute(array(':employeeID' => $employeeID, ':dateToday' => $dateToday));
    } catch(PDOException $e){
        echo'ERROR: ' . $e->getMessage();
    }

if($stmt->fetchColumn() > 0){
    echo "There was a match!";
} else {
    echo "There was no Match";
}

?>

数据库:

+----+------------+----------+------------------+---------------------+-----------+------------+-----------+------------------+
| id | employeeID | date     | timeIn           | jobDescription      | equipType | unitNumber | unitHours | timeOut          |
+----+------------+----------+------------------+---------------------+-----------+------------+-----------+------------------+
|  1 |       NULL | NULL     | 01/06/13 7:16 pm |                     |           | NULL       |      NULL | 01/06/13 8:57 pm |
|  2 |       NULL | NULL     |                  |                     |           | NULL       |      NULL | 01/06/13 7:17 pm |
|  3 |       NULL | NULL     | 01/06/13 8:43 pm |                     |           | NULL       |      NULL | NULL             |
|  4 |       NULL | NULL     | 2:12 am          |                     |           | NULL       |      NULL | 12:47 pm         |
|  5 |       NULL | NULL     | 8:33 pm          |                     |           | NULL       |      NULL | NULL             |
| 17 |       NULL | NULL     | 2:32 pm          |                     |           | NULL       |      NULL | NULL             |
| 18 |       NULL | 01/12/13 | 2:33 pm          |                     |           | NULL       |      NULL | 5:07 pm          |
| 19 |       NULL | 01/12/13 | 2:37 pm          |                     |           | NULL       |      NULL | 5:07 pm          |
| 20 |       NULL | 01/12/13 | 5:07 pm          |                     |           | NULL       |      NULL | 5:07 pm          |
| 21 |          1 | 01/12/13 | 5:31 pm          | Worked in Jefferson | Excavator | 01E        |      8182 | 5:37 pm          |
| 22 |       NULL | NULL     |                  | Worked in Jefferson | Excavator | 01E        |      8932 | NULL             |
| 23 |       NULL | 01/12/13 | 7:10 pm          |                     |           | NULL       |      NULL | NULL             |
| 24 |          1 | 01/12/13 | 7:11 pm          |                     |           | NULL       |      NULL | NULL             |
+----+------------+----------+------------------+---------------------+-----------+------------+-----------+------------------+

1 个答案:

答案 0 :(得分:3)

你可能想要改变='null',因为它寻找字符串'NULL'而不是NULL

$stmt = $conn->prepare("SELECT COUNT(*) FROM timeRecords WHERE `timeOut` IS NULL AND `employeeID`= :employeeID AND `date`= :dateToday");

您也可以尝试:

if($stmt->rowCount()){
    echo "There was a match!";
}else{
    echo "There was no Match";
}