I'm new to php. I've been having trouble connecting to and using a data with PHP. I've double checked and looked on this website for information, but I didn't find much. Here's the error and code below.
I entered my username and password correctly. I even created a new username and password just to make sure. I have no other ideas why it will not connect to my localhost. I would love to see any feedback on possible error and thanks!
The error reads :
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php on line 26
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php:26 Stack trace: #0 C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php(26): PDO->__construct('mysql:host=$hos...') #1 C:\xampp\htdocs\website\Practice\mysqli\pdo.php(5): dbConnect('read') #2 {main} thrown in C:\xampp\htdocs\website\Practice\mysqli\connection.inc.php on line 26 $result = $conn->query($sql)or die(mysqli_error());
connection.ini.php
function dbConnect($usertype, $connectiontype = 'mysqli') {
$host = 'localhost';
$db = 'student';
if ($usertype == 'read') {
$user = 'user';
$pwd = 'pass';
}
elseif ($usertype == 'write') {
$user = 'root';
$pwd = 'password';
}
else {
exit('Unrecognized connection type');
}
//Connection Code
if ($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
}
else {
try {
return new PDO('mysql:host=$host;dbname=$db, $user, $pwd');
}
catch(PDOExecption $e) {
echo 'Cannot connect to database';
exit;
}
}
}
?>
mysqli.php
?php
require_once('connection.inc.php');
$conn = dbConnect('read');
$sql = 'SELECT * FROM guestbook';
$result = $conn->query($sql)or die(mysqli_error());
$numRows = $result->num_rows;
?>
<!DOCTYPE html>
<html>
<p> A total of <?php
echo $numRows;
?>
records were found.</p>
</html>