Bind_parameter error

Hello. I am having an error and it says:
Fatal error : Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\Practice\exam.php:727 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Practice\exam.php on line 727 can anyone help me? I am beginner in PHP. Thank you.

 if (!empty($FirstName) || !empty($MiddleName) || !empty($LastName) || !empty($Age) || !empty($HomeAddress) || !empty($ContactNumber) || !empty($LastSchoolAttended))
    {
    	$host = "localhost";
    	$dbUsername = "root";
    	$dbPassword = "";
    	$dbname = "studentsinformation";
    }
    	$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
    	
    		if (mysqli_connect_error())
    		{
    			die('Connect Error('.mysqli_connect_errno().')'.mysqli_connect_error());
    		}
    		else
    		{	
    			$SELECT = "SELECT FirstName, MiddleName, LastName, LastSchoolAttended From information Where FirstName = ? And MiddleName = ? And LastName = ? And LastSchoolAttended = ? Limit 1";
    			$INSERT	= "INSERT Into information (FirstName, MiddleName, LastName, Age, HomeAddress, ContactNumber, LastSchoolAttended) values (?,?,?,?,?,?,?)";
    			$stmt = $conn->prepare($SELECT);
    			$stmt -> bind_param("ssss", $FirstName,$MiddleName,$LastName,$LastSchoolAttended); **this is where I got an error. LINE 727**
    			$stmt -> execute();
    			$stmt -> bind_result($FirstName,$MiddleName,$LastName,$LastSchoolAttended);
    			$stmt -> store_result();
    			$rnum = $stmt -> num_rows;

    			if ($rnum == 0 ) {	
    				$stmt->close();
    				$stmt = $conn -> prepare($INSERT);
    				$stmt -> bind_param("sssisis", $FirstName,$MiddleName,$LastName,$Age,$HomeAddress,$ContactNumber,$LastSchoolAttended);
    				$stmt -> execute();
    			}
    		}
    	?>

You are mixing between procedural and OOP styles. You can also test if the prepare worked.

if ($conn->connect_errno)

You may also want to turn error reporting on.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
1 Like

Still got the same error.

Then try to test the prepare

$stmt = $conn->prepare($SELECT);
if(!$stmt)
    echo "Prepare failed";
1 Like

The echo showed. What does that mean?

I inserted the if below the existing $stmt = $conn->prepare($SELECT)

OMG. MY MISTAKE. I got the different name from my server and to my PHP. Thank you so much for the help :slight_smile:

1 Like

So turning on the error reporting should have triggered that there was an issue in the connection.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service