'Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. Fatal error: Uncaught Error: [closed]
I'm a beginner in PHP and was trying to access my database after clicking on the login I get the error below while establishing connection
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. Fatal error: Uncaught Error: Call to a member function prepare() on string in C:\xampp\htdocs\simplesite\validate.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\simplesite\validate.php on line 17
Below is my validate.php code
<?php
include_once('connection.php');
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"]== "POST") {
$adminname = test_input($_POST["adminname"]);
$password = test_input($_POST["password"]);
$stmt = $conn->prepare("SELECT * FROM admin_login");
$stmt->execute();
$users = $stmt->fetchAll();
foreach($users as $user) {
if(($user['adminname'] == $adminname) &&
($user['password'] == $password)) {
header("Location: adminpage.php");
}
else {
echo "<script language='javascript'>";
echo "alert('WRONG INFORMATION')";
echo "</script>";
die();
}
}
}
?>
connection.php code
<?php
$conn = "";
try {
$servername = "simplesite";
$dbname = "simplesite";
$username = "root";
$password = "";
$conn = new PDO(
"mysql:host=$servername; dbname=simplesite",
$username, $password
);
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|