Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am facing the below issue.
Quote:
Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, bool given in C:\location:18 Stack trace: #0 C:\location(18): mysqli_query(true, 'select*from `us...') #1 {main} thrown in C:\location on line 18

Please sort it out.
Below is the code.
PHP
<?php

$dbhost="localhost"; 

$dbuser = "root"; 

$dbpass = "password"; 



$mysql =  mysqli_connect($dbhost, $dbuser, $dbpass, 'userlogin');

if($mysql = true){
 echo "Connection Succesful!";
} else{
    die(mysqli_error);
}
if(mysqli_query($mysql, 'select*from `user info`')){
echo "<br> ERROR SOLVED FOREVER!";
}else{
echo "<h1><br><br> THIS IS AN ERROR!".mysqli_error'</h1>'
}
?>

NOTE: The issue is only on line 18. It worked successfully for the connection test.
Thank you.

What I have tried:

Replacing $mysql with mysqli_connect($dbhost, $dbuser, $dbpass, 'userlogin'); instead in line 18 inside the mysqli_query() function. . It worked.
Posted
Updated 7-Jul-21 22:04pm
v2

1 solution

If in doubt, read the manual: PHP: mysqli_connect - Manual[^] It says:
Quote:
If mysqli exception mode is not enabled and a connection fails, then mysqli_connect() returns false instead of an object.

That isn't the same as "if it succeeds, it returns true".
If it succeeds, it returns a connection object you can use, not a boolean value.

Change your test to check for false instead of true
 
Share this answer
 
Comments
Mr.Kcoder 8-Jul-21 4:52am    
Thank you! It worked out well
OriginalGriff 8-Jul-21 5:02am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900