PHP mysqli prepare statement bind_param boolean error

Testing the statement from all side, but failed to find a solution for it.

// Insert the new user into the database 
 if( $insert_stmt = $mysqli->prepare("INSERT INTO client (username, email, 
 password, reg_ip, salt, country, ref_id, pin, ref_by, ref_by_2) VALUES ( ?, 
  ?, ?, ?, ?, ?, ?, ?, ?, ?)")){
     $insert_stmt->bind_param("ssssssssii", $username, $email, $pass_2, 
     $reg_ip, $random_salt, $countryname, $ref_code, $hashed_pin, $user_id3, 
     $user_id4);
     $insert_stmt->execute();

This never executes or gets inside the if statement.

I debugged it by removing the if part, that shows bind_param() is boolean error.

 $insert_stmt = $mysqli->prepare("INSERT INTO client (username, email, 
 password, reg_ip, salt, country, ref_id, pin, ref_by, ref_by_2) VALUES ( ?, 
  ?, ?, ?, ?, ?, ?, ?, ?, ?)");
 $insert_stmt->bind_param("ssssssssii", $username, $email, $pass_2, $reg_ip, 
 $random_salt, $countryname, $ref_code, $hashed_pin, $user_id3, $user_id4);
 if($insert_stmt->execute()){

Fatal error: Call to a member function bind_param() on boolean

I have done following test:

  • All 10 variables data type test = OK (with gettype() function)
  • Variables data value = OK (printed all data value for checking)
  • Mysql query statement = OK (tested on MYSQL directly with inputted data, mysql is inserting values)
  • There is no syntax error either.
  • Variable alignment is = Ok
  • Data connection is = ok (as it runs other prepare statements without errors on same page)

Then where is the mistake?

I figure it out.

Solution:
It was not working because of the previous prepare statement $stmt_aff connection was not closed.
Once I closed it. Next Prepare statement $insert_stmt started working.

A good lesson learned why bind_param boolean error get produced if there are multiple prepare statement on the same page.

$stmt_aff->close();