Home > Back-end >  mysqli_stmt_store_result(): Argument #1 ($statement) must be of type mysqli_stmt, bool
mysqli_stmt_store_result(): Argument #1 ($statement) must be of type mysqli_stmt, bool

Time:11-11

Full Error:

Fatal error: Uncaught TypeError: mysqli_stmt_store_result(): Argument #1 ($statement) must be of type mysqli_stmt, bool given in C:\xampp\htdocs\TableClasses.php:986 Stack trace: #0 C:\xampp\htdocs\TableClasses.php(986): mysqli_stmt_store_result(true) #1 C:\xampp\htdocs\HospAdmin_createStaffValidation.php(62): TableClasses->createDoctor('test', 'test', 'G32434243G', 'Male', '2021-11-09', '234234224', 'test', '[email protected]', 'Anaesthesiology', 'X-ray', '8989', '1_5yr') #2 {main} thrown in C:\xampp\htdocs\TableClasses.php on line 986

I got this error, not sure what's wrong with it

My code

        $allaccounts = mysqli_prepare($this->conn, "SELECT 'patient' as Role, FirstName, LastName, NRIC, password FROM patients WHERE NRIC = ? 
        UNION ALL 
        SELECT 'doctor', FirstName, LastName, NRIC, password FROM doctor WHERE NRIC = ?  
        UNION ALL 
        SELECT 'nurse', FirstName, LastName, NRIC, password FROM nurse WHERE NRIC = ? 
        UNION ALL 
        SELECT 'admin', FirstName, LastName, NRIC, password FROM administrator WHERE NRIC = ?  
        UNION ALL 
        SELECT 'HospitalAdmin', FirstName, LastName, NRIC, password FROM hospitaladmin WHERE NRIC = ?  ");

        mysqli_stmt_bind_param($allaccounts, "sssss", $NRIC, $NRIC, $NRIC, $NRIC, $NRIC);
        $AllAccstmt = mysqli_stmt_execute($allaccounts);
        mysqli_stmt_store_result($AllAccstmt);
        mysqli_stmt_close($allaccounts);
        
        $allaccounts_Email = mysqli_prepare($this->conn, "SELECT 'patient' as Role, FirstName, LastName, NRIC, password FROM patients WHERE Email = ? 
        UNION ALL 
        SELECT 'doctor', FirstName, LastName, NRIC, password FROM doctor WHERE email = ?  
        UNION ALL 
        SELECT 'nurse', FirstName, LastName, NRIC, password FROM nurse WHERE email = ? 
        UNION ALL 
        SELECT 'admin', FirstName, LastName, NRIC, password FROM administrator WHERE email = ?  
        UNION ALL 
        SELECT 'HospitalAdmin', FirstName, LastName, NRIC, password FROM hospitaladmin WHERE email = ?  ");

        mysqli_stmt_bind_param($allaccounts_Email, "sssss", $Email, $Email, $Email, $Email, $Email);
        $AllAcc_Email_stmt = mysqli_stmt_execute($allaccounts_Email);
        mysqli_stmt_store_result($AllAcc_Email_stmt);
        mysqli_stmt_close($allaccounts_Email);
        

        if (mysqli_stmt_num_rows($AllAccstmt) != 0 OR mysqli_stmt_num_rows($AllAcc_Email_stmt) != 0){

I'm trying to check if the select statement is empty. But it keeps giving me error.

CodePudding user response:

As the error message says, mysqli_stmt_store_result() expects mysqli_stmt as an argument. You provided a boolean.

Change the code as follows:

mysqli_stmt_bind_param($allaccounts, "sssss", $NRIC, $NRIC, $NRIC, $NRIC, $NRIC);
mysqli_stmt_execute($allaccounts);
mysqli_stmt_store_result($allaccounts);

Note: it makes no sense to immediately close the prepared statement without doing anything with the data. Remove the close.

The return value of execute() is always a boolean. You don't need this value in your code. You will get the same error for mysqli_stmt_num_rows($AllAccstmt), so change it also.

  • Related