Home » Php » php – Can't connect to database with pdo

php – Can't connect to database with pdo

Posted by: admin February 25, 2020 Leave a comment


This is my code to connect to a database.

$host = 'http://databases.000webhost.com/';
    $dbname = 'myDb';
    $username = 'root';
    $password = 'myPsw';

  try {

    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

    echo "Connecté à $dbname sur $host avec succès.";

  } catch (PDOException $e) {

    die("Impossible de se connecter à la base de données $dbname :" . $e->getMessage());


I followed this tutorial: https://waytolearnx.com/2019/10/connexion-a-une-base-de-donnees-mysql-avec-php-pdo.html

And here is the error that comes up.

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Failed to parse address “http://databases.000webhost.com

How to&Answers:

Use $host = 'databases.000webhost.com';, you just need to provide host name (or IP address), not URL. MySQL doesn’t use HTTP protocol.