Home  >  Q&A  >  body text

php - 数据库有查询结果但pdo查询结果为空?

代码如下:

<?php
$userid=$_POST["user"];
$compassword=$_POST["psw"];

$servername="localhost";
$user="root";
$psw="iiiiiii";
    $con=new PDO("mysql:host=$servername;dbname:mydb",$user,$psw);
    $con->query("set names utf8");
    echo $userid."<br />";//有结果输出
    echo $compassword."<br />";//有结果输出
    $sql="use mydb;select * from admin where userid='$userid';";
    $check=$con->query($sql);
    $numcount=$check->rowCount();
    if($numcount==0){
        echo '用户不存在';
    }else{
        echo '该用户存在';
    }
?>

运行后页面一直显示“用户不存在”,即$numcount为0;
但是我在数据库里面查询的时候是能够查到记录的:

请问是什么原因导致rowCount()得到的结果跟实际结果不一样?该怎么解决?

高洛峰高洛峰2734 days ago990

reply all(2)I'll reply

  • 某草草

    某草草2017-05-16 13:00:03

    The code is written wrong.

    $con=new PDO("mysql:host=$servername;dbname:mydb",$user,$psw);

    changed to

    $con=new PDO("mysql:host=$servername;dbname=mydb",$user,$psw);

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:00:03

    First execute use muDb and run the select statement independently

    reply
    0
  • Cancelreply