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?
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);
First execute use muDb and run the select statement independently