php - The database has query results but the pdo query results are empty?
高洛峰
高洛峰 2017-05-16 12:58:03
0
2
1013

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?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
某草草

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

过去多啦不再A梦

First execute use muDb and run the select statement independently

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template