0

I enable mysql service on XAMPP in my windows PC. No password set into the mysql service, i can connect mysql with MYSQL workbench by Hostname: localhost , Port: 3306 and Username: root

but when i try into my php script below

<?php
$servername = "localhost";
$username = "root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

It gives me Connection failed. What is problem?

KEN IP
  • 13
  • 3

1 Answers1

0

I think that you must send an empty password :

<?php
$servername = "localhost";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
Loïc
  • 11,804
  • 1
  • 31
  • 49