PHP查询执行没有错误但返回计数0

时间:2015-05-15 12:20:06

标签: javascript php mysql angularjs pdo

我在angularjs中创建登录,在login.phppage中我验证用户是否存在行数存在与否意味着在php pdo中使用rowCount()但它总是返回0结果。这是我的php文件:

<?php
   /*
   * Collect all Details from Angular HTTP Request.
   */ 
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $email = $request->email;
    $pass = $request->password;



    $user ="root";
$pass ="m2n1shlko";

$dbh = new PDO('mysql:host=localhost;dbname=blog_db', $user, $pass);

$query = $dbh->prepare("SELECT * FROM `signup_tbl` WHERE `email`='$email' AND `password`='$pass'");
$query->execute();
$count = $query->rowcount();
if($count>0){

echo "You have sucessfully login with this Email: ".$count; //this will go back under "data" of angular call.

}else{


  echo "Error While Authentication with this Email: " .$count;
}


    /*
     * You can use $email and $pass for further work. Such as Database calls.
    */    
?>

来自控制器的数据到达这里我不知道我在哪里错过了代码。 Apreciate如果有帮助..谢谢

3 个答案:

答案 0 :(得分:1)

您将$pass覆盖到数据库(第13行)和用户(第8行)。将数据库密码更改为$db_pass

...
$email = '...';
$pass = $request->password;
...
$db_pass = '...';
...
$dbh = new PDO('mysql:host=localhost;dbname=blog_db', $user, $db_pass); // $pass to $db_pass

答案 1 :(得分:0)

$query->rowCount();不是$query->rowcount();

 $count = $query->rowCount();
 if($count>0){
 echo "You have sucessfully login with this Email: ".$count; //this will go back under "data" of angular call.
 }else{
 echo "Error While Authentication with this Email: " .$count;
 }

阅读手册rowCount

答案 2 :(得分:0)

我已经在我的本地系统中完成了,它运行良好,请尝试下面的示例,这样做很好。只需按照此示例操作并正确执行代码:

<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "xxx";
// Create connection
$conn = new PDO('mysql:host=localhost;dbname=xxx', $username, $password);

$query =$conn->prepare("select * from testing");
$query->execute();
$count = $query->rowCount();
echo "Counting:".$count;
?>

<强>输出: -

Counting:3