Error: mysqli_query() expects

Warning: mysqli_connect(): (HY000/1045): Access denied for user ‘root’@‘localhost’ (using password: YES) in C:\xampp\htdocs\addemp\database.php on line 8

Warning: mysqli_error() expects parameter 1 to be mysqli, bool given in C:\xampp\htdocs\addemp\database.php on line 15
connection not success:
Warning: mysqli_connect(): (HY000/1045): Access denied for user ‘root’@‘localhost’ (using password: YES) in C:\xampp\htdocs\addemp\process.php on line 11

Warning: mysqli_query() expects parameter 1 to be mysqli, bool given in C:\xampp\htdocs\addemp\process.php on line 12

process.php

<?php
include 'database.php';

// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
$bday=$_POST['bday'];
//Execute the query
$connect = mysqli_connect('localhost', 'root', '123', 'mydatabase');
mysqli_query($connect,"INSERT INTO employees1(first_name,last_name,department,email,bday) VALUES('$first_name','$last_name','$department','$email','$bday')");
?>

database.php

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

// Create connection
$connect = mysqli_connect($servername, $username, $password, $database);

if($connect)
{
echo "connection success";
}
else{
echo "connection not success:".mysqli_error($connect);
}

?>
$sql = mysqli_query($connect, "INSERT INTO employees1() VALUES()"); 
if($sql){ echo "Inserted"} else {echo "error";}

You can do this in database.php:

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

I made the changes. now its showing Warning : mysqli_connect(): (HY000/1049): Unknown database ‘mydatabase’ in C:\xampp\htdocs\addemp\database.php on line 8
Failed to connect to MySQL: Unknown database ‘mydatabase’

So what’s unclear about the message? It’s pretty obvious what the problem is, so check if you connected to the right server, check you already have a database with this name, and check that the user has the needed permissions.

1 Like

Try this what I did:
<?php
$servername = “localhost”;
$username = “root”;
$password = “123”;
$database = “mydatabase”;
$conn = mysqli_connect($servername, $username, $password, $database);
if($conn){
echo “Connection Successfull”;
}
else{ echo “Failed to connect”. mysqli_connect_error();}

Your error wasn’t that you were using bad credentials. The error says exactly what the issue is, and them blindly copying and pasting the code shows no effort, as proven by the last error they received by trying to use ‘mydatabase’.

Sponsor our Newsletter | Privacy Policy | Terms of Service