SQLSTATE [HY000] [2002]没有这样的文件或目录 - Php函数

时间:2016-09-26 12:54:54

标签: php sql function pdo

我的代码有问题。我试图用pdo和php创建一个登录功能。当我尝试登录时,我发出消息:SQLSTATE [HY000] [2002]没有这样的文件或目录。

如果我没有创建一个函数(),只需将代码放在同一页面上即可。

我真的想了解这个问题,感谢所有帮助。

登录页面

<?php
include_once("../data/functions/index.php");


if(isset($_POST['login-btn'])) {

$email = strip_tags($_POST['email']);   
$password = strip_tags($_POST['password']);

DoLogin($email,$password);

}   



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LOL</title>
</head>
<body>

<div class="signin-form">

    <div class="container">


       <form class="form-signin" method="post" id="login-form">

        <div class="form-group">
        <input type="email" class="form-control" name="email" placeholder="E-mail" />
        </div>

        <div class="form-group">
        <input type="password" class="form-control" name="password" placeholder="Your Password" />
        </div>


        <div class="form-group">
            <button type="submit" name="login-btn" class="btn btn-default">
                Log in
            </button>
        </div>  

      </form>

    </div>

</div>




</body>
</html>

功能文件

<?php

require_once("../data/connection/connect.php"); 

function DoLogin($email,$password) {

try
        {
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $uname, $pass);
          $stmt = $conn->prepare("SELECT * FROM users WHERE user_email = :email");
          $stmt->bindParam(':email', $email);
          $stmt->execute();
          $row = $stmt->fetch();

          $count = $stmt->rowCount();

          if($count > 0 && password_verify($password, $row['user_password'])) {             


            echo "YEEEEAAAAA BOOOOOIIII";

            }
            else {

                echo "fail :'(";
            }


        }



        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

    }




?>

数据库连接文件

<?php
$servername = "x";
$uname = "x";
$pass = "x";
$dbname = "x";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $uname, $pass);
    // set the PDO error mode to exception   
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

    $conn == null;    

?>

0 个答案:

没有答案