Jelles
Jelles

Reputation: 25

Connection error MySQL

I copied this from W3Schools and when I try to run it it gives me the a error. I am really new to MySQL so I am trying to fix this but I don't know how.

ERROR:

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

<html>
<body>

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

// Create connection
$conn = new mysqli($servername, $username, $password);

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

</body>
</html>

Upvotes: 1

Views: 673

Answers (4)

ikppramesh
ikppramesh

Reputation: 17

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass); 

//Connection to mysql check

if(! $conn ) {
         die('Could not connect: ' . mysql_error()); 
         }

mysqli_select_db($conn,'databaseName');

?>

Upvotes: 0

Saquib Lari
Saquib Lari

Reputation: 190

your username or password is incorrect if you are very new and just starting with the local server try

$username = "root";

$password = "";

as your username and password

Upvotes: 2

Ingus
Ingus

Reputation: 1044

you have to add correct information provided by your hosting provider here :

$servername = "localhost";
$username = "username";
$password = "password";

Upvotes: 0

Arsalan Mithani
Arsalan Mithani

Reputation: 480

W3schools don't know your localhost details, do the following

$servername = "localhost";
$username = "root"; //put your username here
$password = "";   // your password here

Leave password empty and put "root" in username, that's the default xampp setting.

Upvotes: 2

Related Questions