检查空结果(php,pdo,mysql)

时间:2012-11-20 16:58:56

标签: php mysql pdo

拜托,谁能告诉我这里我做错了什么?我只是从表中检索结果然后将它们添加到数组中。一切都按预期工作,直到我检查一个空的结果......

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

$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 ;

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

$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 ;

与往常一样,任何帮助表示赞赏。谢谢!

8 个答案:

答案 0 :(得分:86)

执行$sth->fetchColumn()时,您将丢弃结果行。这不是你如何检查是否有任何结果。你做了

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

相关文档:http://php.net/manual/en/pdostatement.rowcount.php

答案 1 :(得分:10)

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

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

将返回返回的行数。

答案 2 :(得分:7)

虽然这是一个老话题,但我认为我会因为最近不得不处理这个问题而权衡。

您不应将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 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;

当然,这只适用于单个结果,就像循环数据集时一样。

答案 3 :(得分:6)

$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!';
}

答案 4 :(得分: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变量),以判断您的查询是否返回任何内容。请阅读我的article on PDO

答案 5 :(得分:1)

另一种需要考虑的方法:

当我构建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)) {

答案 6 :(得分:0)

感谢Marc B的帮助,这里有什么对我有用(注意:Marc的rowCount()建议也可以起作用,但是我不满意它不能在不同的DB上工作或者我的内容发生了变化的可能性。 ..而且,他的选择计数(*)建议也会起作用,但是,我想,因为如果它存在的话,我最终会获得数据,所以我就这样了。)

$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 ;

答案 7 :(得分:0)

我只发现一种可行的方法...

$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;
}