如何知道SQL查询PDO是否为空?

时间:2015-02-22 17:59:26

标签: php pdo

我开始绝望,因为我没有条件,如果PDO请求的结果是空的......这是我使用的代码:

try
{
    $pdo=new PDO('mysql:host=localhost;dbname=MyDataBase','root','');
}
catch (Exception $e)
  {
    die('Erreur : ' . $e->getMessage());
  }

$sql = "SELECT COUNT(*) FROM Mytable WHERE name=".$name;

if ($res = $bdd->query($sql)){
  echo" this name exist ";
}
else {
  echo "No rows matched the query.";
}

1 个答案:

答案 0 :(得分:0)

Ues this ...

     $name= $_POST['name'];


        try
        {
            $pdo=new PDO('mysql:host=localhost;dbname=MyDataBase','root','');
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (Exception $e)
          {
            die('Erreur : ' . $e->getMessage());
          }

        $sql = "SELECT COUNT(*) FROM Mytable WHERE name=".$name;
        $res = $pdo->query($sql);
        $row = $res->fetchColumn();
        if ($row){
          echo" this name exist ";
    }
       else {
          echo "No rows matched the query.";
       }

它应该$pdo而不是$bdd在查询语句中...基本上是一个错字...所有