0

I'm trying to connect my PHP to my XAMPP DB server, but as it seems I'm not doing something right. This is my code:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$db=  "blogdata";
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

And it returns:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\xampp\htdocs\KungFu\Blog.php on line 8 Connection failed: Access denied for user 'username'@'localhost' (using password: YES)

I think everything is correct, tried different variations but as you can tell it is not working.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Using your username/password details, are you able to log into phpmyadmin? Did you create this new user profile in mysql too? Sorry might sound like a dumb question here. Maybe reset the password via phpmyadmin. – PHPology Jan 05 '20 at 15:55
  • Ahh did you assign the database to this username? – PHPology Jan 05 '20 at 15:56
  • I am using XAMMP, therefor i never set a username or pass, its the default –  Jan 05 '20 at 17:06
  • Ok so with that username and password, can you access PHPmyadmin then? – PHPology Jan 05 '20 at 17:13
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Jan 05 '20 at 18:02
  • I am sayin that i use XAMMP, which means i dont log in, the software does it, i only press admin –  Jan 05 '20 at 18:11

1 Answers1

-2

Best way to connect php to MySQL is to use new method : PDO :)

PHP : $bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');

  • ok, thanks, but in this case i want to use the MySQLi package, and do you have any idea to fix it ? –  Jan 05 '20 at 18:12