0
votes

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

1
try $host = 'localhost' - Rhalp Darren Cabrera
but db is hosted in databases.000webhost.com not on my computer that's why i change localhost - thomas
just try it.. I've done that in the past.. even it was hosted in hosting site like 000wehost, they still use the $host = 'localhost'. if nothing happen atleast you try if that method is still working. - Rhalp Darren Cabrera
Here is a much better tutorial: phpdelusions.net/pdo - Dharman

1 Answers

2
votes

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