php mysql pdo如果数据库中没有数据则返回错误消息

时间:2013-12-29 06:08:15

标签: php mysql pdo

我想逻辑是如果数据库中有数据然后查询数据。否则,如果没有数据,则会显示错误消息。

这是我的代码:

$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
    echo 'No data found in database!';
} else {
    return $data = $query;
}

在此代码之前,如果是从数据库查询的代码:

//Query string and put it in a variable.
if(isset($_POST['dob_chi'])){
    $query = "SELECT * FROM $table_n WHERE dob_chi = :date";
} else {
$query = "SELECT * FROM $table_n WHERE dob_eng = :date";
}

我尝试输入非数据来执行代码但不知何故它没有向脚本区域显示错误和直接进程。

以下脚本:

//create a while loop for every entry in our DB where the date is match.
while ($row = $stmt->fetchObject()) {
    $r1          = $row->rowone;
    $r2          = $row->rowtwo;
    $r3          = $row->rowthree;
    $englishdate = $row->dob_eng;
    $chinesedate = $row->dob_chi;

    //add all initial data into the matrix variable for easier access to them later
$rows[0]
    $rows = array(
        array($r1),
        array($r2),
        array($r3),
        array()
    );

}
//incoporate modulo value as an argument.
function incmod($a, $m)
{
    return ($a % $m) + 1;
}

//Population each row, with $populationCount number of elements, where each element is added with incmod(X, $mod)
function populateRow($rowId, $populationCount, $mod)
{
    //function to access the global variable.
    global $rows;
    $row = $rows[$rowId];
    while (sizeof($row) < $populationCount) {
        $rowInd = sizeof($row) - 1;
        $m      = incmod($row[$rowInd], $mod);
        array_push($row, $m);
    }

    //set the row back into the global variable.
    $rows[$rowId] = $row;
}

1 个答案:

答案 0 :(得分:1)

$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
    echo 'No data found in database!';
}