0

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>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Raymond Jones
  • 43
  • 1
  • 8
  • possible duplicate of [mysql\_connect: php\_network\_getaddresses: getaddrinfo failed: No such host is known using file values](http://stackoverflow.com/questions/21876269/mysql-connect-php-network-getaddresses-getaddrinfo-failed-no-such-host-is-kno) – Jay Blanchard Jul 15 '15 at 14:10

2 Answers2

2
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(PDOException $e) {
        echo 'Cannot connect to database';
        exit;
    }
}
}
?>

You were using

 return new PDO('mysql:host=$host;dbname=$db, $user, $pwd');

But you need to use double quotes to use variables like

return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);

or you can use this :

return new PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd);

You can see more here : Pdo Connection and Variable inside double quotes

Community
  • 1
  • 1
Rox
  • 811
  • 6
  • 20
2

Single quote enclosed strings are not parsed, so variables inside it are not replaced by their value. Use double quotes instead of single quotes in the DSN string:

return new PDO("mysql:host=$host;dbname=$db, $user, $pwd");