54

我在这里做错了什么?我只是从表中检索结果,然后将它们添加到数组中。在我检查空结果之前,一切都按预期工作......

这将获得匹配,将其添加到我的数组并按预期回显结果:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today', $today, PDO::PARAM_STR);

if(!$sth->execute()) {
    $db = null;
    exit();
}

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    $this->id_email[] = $row['id_email'];
    echo $row['id_email'];
}

$db = null;
return true;

当我尝试检查空结果时,我的代码返回“空”,但不再产生匹配结果:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today, PDO::PARAM_STR);

if(!$sth->execute()) {
    $db = null;
    exit();
}

if ($sth->fetchColumn()) {
    echo 'not empty';
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        $this->id_email[] = $row['id_email'];
        echo $row['id_email'];
    }
    $db = null;
    return true;
}
echo 'empty';
$db = null;
return false;
4

8 回答 8

102

当您这样做时,您正在丢弃结果行$sth->fetchColumn()。这不是您检查是否有任何结果的方式。你做

if ($sth->rowCount() > 0) {
  ... got results ...
} else {
   echo 'nothing';
}

相关文档在这里:PDOStatement::rowCount

于 2012-11-20T17:01:28.260 回答
14

如果您可以选择使用 fetchAll() 那么,如果没有返回行,它将只是一个空数组。

count($sql->fetchAll(PDO::FETCH_ASSOC))

将返回返回的行数。

于 2016-05-16T14:40:22.587 回答
11

您不应将 rowCount 用于 SELECT 语句,因为它不可移植。我使用isset函数来测试 select 语句是否有效:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

// I would usually put this all in a try/catch block, but I kept it the same for continuity
if(!$sth->execute(array(':today'=>$today)))
{
    $db = null;
    exit();
}

$result = $sth->fetch(PDO::FETCH_OBJ)

if(!isset($result->id_email))
{
    echo "empty";
}
else
{
    echo "not empty, value is $result->id_email";
}

$db = null;

当然,这仅适用于单个结果,就像您在遍历数据集时可能遇到的那样。

我想我会权衡一下,因为我最近不得不处理这个问题。

于 2014-12-02T22:03:58.967 回答
9
$sql = $dbh->prepare("SELECT * from member WHERE member_email = '$username' AND member_password = '$password'");

$sql->execute();

$fetch = $sql->fetch(PDO::FETCH_ASSOC);

// if not empty result
if (is_array($fetch))  {
    $_SESSION["userMember"] = $fetch["username"];
    $_SESSION["password"] = $fetch["password"];
    echo 'yes this member is registered'; 
}else {
    echo 'empty result!';
}
于 2015-03-15T22:09:13.800 回答
3

我在这里做错了什么?

几乎所有的。

$today = date('Y-m-d'); // no need for strtotime

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today); // no need for PDO::PARAM_STR

$sth->execute(); // no need for if
$this->id_email = $sth->fetchAll(PDO::FETCH_COLUMN); // no need for while

return count($this->id_email); // no need for the everything else

实际上,您始终拥有获取的数据(在本例中为$this->id_email变量)来判断您的查询是否返回任何内容。在我关于 PDO 的文章中阅读更多内容。

于 2016-05-16T15:03:44.403 回答
2

另一种需要考虑的方法:

当我构建 HTML 表或其他依赖于数据库的内容时(通常通过 AJAX 调用),我喜欢在处理任何标记之前检查 SELECT 查询是否返回了任何数据。如果没有数据,我只是返回“未找到数据......”或类似的东西。如果有数据,则继续,构建标题并循环遍历内容等。尽管我可能会将我的数据库限制为 MySQL,但我更喜欢编写可移植的代码,所以 rowCount() 不可用。相反,请检查列数。不返回任何行的查询也不会返回任何列。

$stmt->execute();
$cols = $stmt->columnCount(); // no columns == no result set
if ($cols > 0) {
    // non-repetitive markup code here
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
于 2015-05-31T09:40:32.443 回答
1

我只找到了一种有效的方法...

$quote = $pdomodel->executeQuery("SELECT * FROM MyTable");

//if (!is_array($quote)) {  didn't work
//if (!isset($quote)) {  didn't work

if (count($quote) == 0) {   //yep the count worked.
    echo 'Record does not exist.';
    die;
}
于 2019-11-02T17:44:29.127 回答
0

感谢Marc B 的帮助,这对我有用(注意:Marc 的 rowCount() 建议也可以工作,但我对它不能在不同的数据库上工作或者我的某些东西发生变化的可能性感到不舒服......也,他的 select count(*) 建议也可以,但是,我想是因为如果数据存在的话,我最终会得到数据,所以我就这样走了)。

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today', $today, PDO::PARAM_STR);

if(!$sth->execute()) {
    $db = null;
    exit();
}

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    $this->id_email[] = $row['id_email'];
    echo $row['id_email'];
}
$db = null;

if (count($this->id_email) > 0) {
    echo 'not empty';
    return true;
}
echo 'empty';
return false;
于 2012-11-20T17:51:05.743 回答