I have a problem with my code. I am trying to create a login function with pdo and php. When I try to login I geat the message: SQLSTATE[HY000] [2002] No such file or directory.
If I don't make a function() and just have the code on the same page it works.
I really trying to understand this issue and all help are appreciated.
Login Page
<?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>
Functions file
<?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();
}
}
?>
DB connection file
<?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;
?>