Jump to content

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


CadJoe
Go to solution Solved by CadJoe,

Recommended Posts

2 9 0 1

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

 

It fails on the BIND_PARAM line.

 

I have looked at this all day, struggling with Mysqli as my normal MySql works fine with assoc and fetch.

 

I have verified in PhpInfo that mysqli is enabled.

I have verified its loaded using <?php print_r(get_loaded_extensions());

Added utf8 just to make sure. 

 

If I manually use the Wrong Password, I get a connection refused error, so I know its connected.

 

Verified my $_POST variables are there using Print in the 2 9 0 1 above.

 

All Table and Field Names are correct.  Manual Insert works fine in myPhpAdmin.

Tried Field Names and ? marks with and without single quotes.

All fields are Integers in the table.

 

Could it be related to an Auto_Increment Field in the table for a record ID that I am not providing??

A normal insert in myPhpAdmin doesn't need it.

 

 

        $QuestionID = $_POST['qID'];
        $PlayerID = $_POST['pID'];        
        $Answer = $_POST['ans'];
        $Option = $_POST['opt'];
        

        print $QuestionID . " ";
        print $PlayerID . " ";
        print $Answer . " ";
        print $Option . " ";

        $database = "Survey";

        $db_found = new mysqli('localhost', 'survey', 'survey', $database);
        $db_found->set_charset("utf8");
        
        if (!$db_found) {
            echo "Error: db connection = false";
        }
        else {
            $SQL_INSERT = "INSERT INTO 'Answers' ('QuestionID', 'PlayerID', 'Answer', 'Option') VALUES (?,?,?,?)";
            $stmt = $db_found->prepare($SQL_INSERT);
            $stmt->bind_param('iiii', $QuestionID, $PlayerID, $Answer, $Option);
            $stmt->execute();
        }

 

Thanks,

CadJoe

Edited by CadJoe
Link to comment
Share on other sites

  • Solution

ok, I have it working now.

 

Knew it was something with the SQL statement, but couldn't figure out what.

 

I added this to the top <?php

 

error_reporting(E_ALL);
ini_set('display_errors', 1);

 

 

I changed the Prepare statement to include the error which lead me to error 1064.

Took the single quotes off of the Field Names, and it started working.

 

 

Adding the below IF statement is HUGE to see the errors.

 

 

       $database = "Survey";

        $db_found = new mysqli('localhost', 'survey', 'survey', $database);
        $db_found->set_charset("utf8");
        
        
        if (!$db_found) {
            echo "Error: db connection = false";
        }
        else {
            $SQL_INSERT = "INSERT INTO Answers (QuestionID, PlayerID, Answer, Option) VALUES (?,?,?,?)";
            $stmt = $db_found->prepare($SQL_INSERT);

            if($stmt = $db_found->prepare($SQL_INSERT)) {        
                $stmt->bind_param('iiii', $QuestionID, $PlayerID, $Answer, $Option);
                $stmt->execute();
                $voteMessage = "Thanks for voting";
            } else {
                $error = $db_found->errno . ' ' . $db_found->error;
                echo $error;

            }
        } // else

 

 

 

 

Really hope this HELPS someone else, as I didn't see this anywhere except one link out of hundreds.

 

Thanks,

 

CadJoe

Edited by CadJoe
Link to comment
Share on other sites

  • 2 years later...

You are a star CadJoe! 🐵

This "Call to a member function bind_param() on boolean" error message was driving me crazy, the more that I didn't have any boolean in bind_param values, so didn't understand what this was all about.

As you are saying, adding the IF statement to catch the errors is a true life-saver - I saw my problem straigt away - I have mismatched a field name (query vs table).

Thanks a lot, you helped me 3 years later! 🐵

EasyCoder

Edited by EasyCoder
Link to comment
Share on other sites

The original query would have worked if the proper mysql column name delimiter was used.  For column or table names, you delimit them using the backtic character, not the single or double quotes.

Wrong:

$SQL_INSERT = "INSERT INTO 'Answers' ('QuestionID', 'PlayerID', 'Answer', 'Option') VALUES (?,?,?,?)";

Right

$SQL_INSERT = "INSERT INTO `Answers` (`QuestionID`, `PlayerID`, `Answer`, `Option`) VALUES (?,?,?,?)";

 

Of course you do not need to delimit table or column names in most cases, but there are cases where you must delimit them, in particular if the name is a mysql keyword.  

Example:

CREATE TABLE USER_LIST (id INTEGER UNSIGNED, CURRENT_USER varchar(40));

This won't work, because CURRENT_USER is a MySQL keyword.

 

This works:

CREATE TABLE USER_LIST (id INTEGER UNSIGNED, `CURRENT_USER` varchar(40));

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.