1

我有下面的 PDO 语句,我试图查找是否有与该语句匹配的行,如果为真则返回 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             |
+----+------------+----------+------------------+---------------------+-----------+------------+-----------+------------------+
4

1 回答 1

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";
}
于 2013-01-13T02:26:41.803 回答