PHP:PDO为什么这段代码不起作用?

PHP:PDO为什么这段代码不起作用?,php,mysql,pdo,Php,Mysql,Pdo,我不明白为什么它不工作 $q1=$conn->prepare('select * from users where username = :data'); $q1->bindParam(':data',$searchdata);//$searchdata is having the value $q1->execute(); if($q1->rowCount()<1) { die('NO results fou

我不明白为什么它不工作

$q1=$conn->prepare('select * from users where username = :data');
    $q1->bindParam(':data',$searchdata);//$searchdata is having the value
    $q1->execute();

       if($q1->rowCount()<1)
    {
        die('NO results found');
    }
    $row=$q1->fetch(PDO::FETCH_ASSOC);
    echo $row['user_id'];

为什么每次我都得到空的结果?这个代码有什么错误吗。请提供帮助。

MySQL要求您使用,因为
rowCount()
方法在获取所有行之前无法知道结果集中有多少行。

试试这个

$rowCount = $pdo->query('select count(*) from blah')->fetchColumn(); 

 if(count($rowCount) > 0)
 {
       //Your code here
 } else {
     die();
 }

$searchdata
有什么值?可能有很多错误,您没有提供足够的信息。$searchdata在方法rowCount()上具有php.net中的值“akbar”:如果关联的PDO语句执行的最后一条SQL语句是SELECT语句,则某些数据库可能会返回该语句返回的行数。然而,并非所有数据库都能保证这种行为,便携式应用程序也不应依赖这种行为。“也许是这样,你使用什么数据库?
$rowCount = $pdo->query('select count(*) from blah')->fetchColumn(); 

 if(count($rowCount) > 0)
 {
       //Your code here
 } else {
     die();
 }