search

Home  >  Q&A  >  body text

php - The database has query results but the pdo query results are empty?

The code is as follows:

<?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 '该用户存在';
    }
?>

After running, the page always displays "User does not exist", that is, $numcount is 0;
But when I query in the database, I can find the record:

What is the reason why the result obtained by rowCount() is different from the actual result? How to solve it?

高洛峰高洛峰2831 days ago1074

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