I need help to solve a problem I can't get rid of. I get this error:
Unable to connect to the database server: SQLSTATE[HY000] [2005] Unknown MySQL server host '$host' (2)

So, the error check works, it writes out the error message. The problem is I can't find the problem/error in the script.
I hope you could be so kind and find it for me. I am VERY sure my user details are correct. Is every reference to
variables correct, for instance? Could you be kind and tell me where and what to do?

<?php
$host = 'mysqlhost.no'; //Default database host.
$dbname = 'mydatabasename'; //Default database name.
$username = 'myusername'; //Default database username.
$password = 'mysecretpassword'; //Default database password.
try {
$pdo = new PDO('mysql:host=$host;dbname=$dbname;charset=utf8', '$username', '$password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $db;
}
catch (PDOException $e) {
$output = 'Unable to connect to the database server<X>: ' . $e->getMessage();
' in ' .$e->getFile() . ':' . $e->getline();
}

    It's because you used single-quotes around your DNS string, so it literally tried to connect to a host named "$host" and a database named "$dbname", not the values of those two variables. Once you fix that by using double-quotes instead, you'll have the same problem with user name and password unless/until you remove the quotes around those two arguments (or change them to double-quotes, but there's no need to quote them at all in that context).

    This was the bit of the error message that made the light bulb go off for me:

    "Unknown MySQL server host '$host'"

      Write a Reply...