代码如下:
<?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()得到的结果跟实际结果不一样?该怎么解决?
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