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











up vote
48
down vote

favorite
4












I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error:



Fatal error: Call to a member function bind_param() on boolean in C:xampp2htdocsapplicationclassesclass.functions.php on line 16


Normally, this would mean that I'm selecting stuff from unexisting tables and stuff. But in this case, I 'm not...



Here's the getSetting function:



public function getSetting($setting)
{
$query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
$query->bind_param('s', $setting);
$query->execute();
$query->bind_result($value, $param);
$query->store_result();
if ($query->num_rows() > 0)
{
while ($query->fetch())
{
return $value;
if ($param === '1')
{
$this->tpl->createParameter($setting, $value);
}
}
}
else
{
__('invalid.setting.request', $setting);
}
}


The $this->db variable is passed through a constructor. In case of need, here is it:



public function __construct($db, $data, $tpl)
{
$this->db = $db;
$this->tpl = $tpl;
$this->data = $data;
$this->data->setData('global', 'theme', $this->getSetting('theme'));
}


Also, since I'm making use of a database, my database connection:



class Database
{
private $data;

public function __construct($data)
{
$this->data = $data;
$this->conn = new MySQLi(
$this->data->getData('database', 'hostname'),
$this->data->getData('database', 'username'),
$this->data->getData('database', 'password'),
$this->data->getData('database', 'database')
);
if ($this->conn->errno)
{
__('failed.db.connection', $this->conn->errno);
}
date_default_timezone_set('Europe/Amsterdam');
}


I've already tested the connection, 100% positive that it works as intended.
I'm setting the DB connection things in a configuration file:



'database' => array(
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => ******,
'database' => 'wscript'
)


Now the weird thing is; the table exists, the requested setting exists, the DB exists, but still, that error won't leave. Here's some proof that the DB is correct:



IMG










share|improve this question




























    up vote
    48
    down vote

    favorite
    4












    I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error:



    Fatal error: Call to a member function bind_param() on boolean in C:xampp2htdocsapplicationclassesclass.functions.php on line 16


    Normally, this would mean that I'm selecting stuff from unexisting tables and stuff. But in this case, I 'm not...



    Here's the getSetting function:



    public function getSetting($setting)
    {
    $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
    $query->bind_param('s', $setting);
    $query->execute();
    $query->bind_result($value, $param);
    $query->store_result();
    if ($query->num_rows() > 0)
    {
    while ($query->fetch())
    {
    return $value;
    if ($param === '1')
    {
    $this->tpl->createParameter($setting, $value);
    }
    }
    }
    else
    {
    __('invalid.setting.request', $setting);
    }
    }


    The $this->db variable is passed through a constructor. In case of need, here is it:



    public function __construct($db, $data, $tpl)
    {
    $this->db = $db;
    $this->tpl = $tpl;
    $this->data = $data;
    $this->data->setData('global', 'theme', $this->getSetting('theme'));
    }


    Also, since I'm making use of a database, my database connection:



    class Database
    {
    private $data;

    public function __construct($data)
    {
    $this->data = $data;
    $this->conn = new MySQLi(
    $this->data->getData('database', 'hostname'),
    $this->data->getData('database', 'username'),
    $this->data->getData('database', 'password'),
    $this->data->getData('database', 'database')
    );
    if ($this->conn->errno)
    {
    __('failed.db.connection', $this->conn->errno);
    }
    date_default_timezone_set('Europe/Amsterdam');
    }


    I've already tested the connection, 100% positive that it works as intended.
    I'm setting the DB connection things in a configuration file:



    'database' => array(
    'hostname' => '127.0.0.1',
    'username' => 'root',
    'password' => ******,
    'database' => 'wscript'
    )


    Now the weird thing is; the table exists, the requested setting exists, the DB exists, but still, that error won't leave. Here's some proof that the DB is correct:



    IMG










    share|improve this question


























      up vote
      48
      down vote

      favorite
      4









      up vote
      48
      down vote

      favorite
      4






      4





      I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error:



      Fatal error: Call to a member function bind_param() on boolean in C:xampp2htdocsapplicationclassesclass.functions.php on line 16


      Normally, this would mean that I'm selecting stuff from unexisting tables and stuff. But in this case, I 'm not...



      Here's the getSetting function:



      public function getSetting($setting)
      {
      $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
      $query->bind_param('s', $setting);
      $query->execute();
      $query->bind_result($value, $param);
      $query->store_result();
      if ($query->num_rows() > 0)
      {
      while ($query->fetch())
      {
      return $value;
      if ($param === '1')
      {
      $this->tpl->createParameter($setting, $value);
      }
      }
      }
      else
      {
      __('invalid.setting.request', $setting);
      }
      }


      The $this->db variable is passed through a constructor. In case of need, here is it:



      public function __construct($db, $data, $tpl)
      {
      $this->db = $db;
      $this->tpl = $tpl;
      $this->data = $data;
      $this->data->setData('global', 'theme', $this->getSetting('theme'));
      }


      Also, since I'm making use of a database, my database connection:



      class Database
      {
      private $data;

      public function __construct($data)
      {
      $this->data = $data;
      $this->conn = new MySQLi(
      $this->data->getData('database', 'hostname'),
      $this->data->getData('database', 'username'),
      $this->data->getData('database', 'password'),
      $this->data->getData('database', 'database')
      );
      if ($this->conn->errno)
      {
      __('failed.db.connection', $this->conn->errno);
      }
      date_default_timezone_set('Europe/Amsterdam');
      }


      I've already tested the connection, 100% positive that it works as intended.
      I'm setting the DB connection things in a configuration file:



      'database' => array(
      'hostname' => '127.0.0.1',
      'username' => 'root',
      'password' => ******,
      'database' => 'wscript'
      )


      Now the weird thing is; the table exists, the requested setting exists, the DB exists, but still, that error won't leave. Here's some proof that the DB is correct:



      IMG










      share|improve this question















      I'm busy on a function that gets settings from a DB, and suddenly, I ran into this error:



      Fatal error: Call to a member function bind_param() on boolean in C:xampp2htdocsapplicationclassesclass.functions.php on line 16


      Normally, this would mean that I'm selecting stuff from unexisting tables and stuff. But in this case, I 'm not...



      Here's the getSetting function:



      public function getSetting($setting)
      {
      $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
      $query->bind_param('s', $setting);
      $query->execute();
      $query->bind_result($value, $param);
      $query->store_result();
      if ($query->num_rows() > 0)
      {
      while ($query->fetch())
      {
      return $value;
      if ($param === '1')
      {
      $this->tpl->createParameter($setting, $value);
      }
      }
      }
      else
      {
      __('invalid.setting.request', $setting);
      }
      }


      The $this->db variable is passed through a constructor. In case of need, here is it:



      public function __construct($db, $data, $tpl)
      {
      $this->db = $db;
      $this->tpl = $tpl;
      $this->data = $data;
      $this->data->setData('global', 'theme', $this->getSetting('theme'));
      }


      Also, since I'm making use of a database, my database connection:



      class Database
      {
      private $data;

      public function __construct($data)
      {
      $this->data = $data;
      $this->conn = new MySQLi(
      $this->data->getData('database', 'hostname'),
      $this->data->getData('database', 'username'),
      $this->data->getData('database', 'password'),
      $this->data->getData('database', 'database')
      );
      if ($this->conn->errno)
      {
      __('failed.db.connection', $this->conn->errno);
      }
      date_default_timezone_set('Europe/Amsterdam');
      }


      I've already tested the connection, 100% positive that it works as intended.
      I'm setting the DB connection things in a configuration file:



      'database' => array(
      'hostname' => '127.0.0.1',
      'username' => 'root',
      'password' => ******,
      'database' => 'wscript'
      )


      Now the weird thing is; the table exists, the requested setting exists, the DB exists, but still, that error won't leave. Here's some proof that the DB is correct:



      IMG







      php mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 23 '17 at 22:29









      ROMANIA_engineer

      32.2k19141140




      32.2k19141140










      asked Dec 10 '14 at 6:39









      Wesley Peeters

      90421838




      90421838
























          10 Answers
          10






          active

          oldest

          votes

















          up vote
          97
          down vote



          accepted










          The problem lies in:



          $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
          $query->bind_param('s', $setting);


          The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?



          Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)






          share|improve this answer



















          • 2




            Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
            – Adam
            May 11 '16 at 13:01






          • 1




            @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
            – RobP
            Dec 13 '16 at 14:53




















          up vote
          17
          down vote













          Any time you get the...




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




          ...it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!



          First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:



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


          If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.



          Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:



          $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
          $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
          $query->bind_param('s', $definition);
          $query->execute();


          The error is generic and not very helpful to you in solving what is going on.



          With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.



          $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
          if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
          $query->bind_param('s', $definition);
          $query->execute();
          // any additional code you need would go here.
          } else {
          $error = $mysqli->errno . ' ' . $mysqli->error;
          echo $error; // 1054 Unknown column 'foo' in 'field list'
          }


          If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.



          If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.






          share|improve this answer

















          • 4




            Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
            – Dawid Ferenczy
            Nov 27 '15 at 17:04


















          up vote
          11
          down vote













          prepare return a boolean only when it fails therefore FALSE, to avoid the error you need to check if it is True first before executing:



          $sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
          if($query = $this->db->conn->prepare($sql)){
          $query->bind_param('s', $setting);
          $query->execute();
          //rest of code here
          }else{
          //error !! don't go further
          var_dump($this->db->error);
          }





          share|improve this answer




























            up vote
            6
            down vote













            Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
            Always close your previous statement with



            $statement->close();


            If the syntax is correct, the following query will run well too.






            share|improve this answer





















            • This answer helped me.
              – Amine
              Jan 4 at 17:23


















            up vote
            1
            down vote













            Another situation that can cause this problem is incorrect casting in your queries.



            I know it may sound obvious, but I have run into this by using tablename instead of Tablename. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.






            share|improve this answer






























              up vote
              0
              down vote













              Sometimes, it is also because of a wrong table name or column name in the prepare statement.



              See this:



              https://www.youtube.com/watch?v=LGHdQ1bBho8






              share|improve this answer




























                up vote
                0
                down vote













                You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.






                share|improve this answer




























                  up vote
                  0
                  down vote













                  This particular error has very little to do with the actual error. Here is my similar experience and the solution...
                  I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...






                  share|improve this answer




























                    up vote
                    0
                    down vote













                    Following two are the most probable cause of this issue:




                    1. Spelling mistake either in column names or table name

                    2. Previously established statement is not closed. Just close it before making the prepared statement.




                    $stmt->close(); // <<<-----This fixed the issue for me

                    $stmt = $conn->prepare("Insert statement");





                    share|improve this answer




























                      up vote
                      0
                      down vote













                      Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
                      INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?) may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)






                      share|improve this answer























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27394710%2ffatal-error-call-to-a-member-function-bind-param-on-boolean%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        10 Answers
                        10






                        active

                        oldest

                        votes








                        10 Answers
                        10






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes








                        up vote
                        97
                        down vote



                        accepted










                        The problem lies in:



                        $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
                        $query->bind_param('s', $setting);


                        The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?



                        Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)






                        share|improve this answer



















                        • 2




                          Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                          – Adam
                          May 11 '16 at 13:01






                        • 1




                          @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                          – RobP
                          Dec 13 '16 at 14:53

















                        up vote
                        97
                        down vote



                        accepted










                        The problem lies in:



                        $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
                        $query->bind_param('s', $setting);


                        The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?



                        Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)






                        share|improve this answer



















                        • 2




                          Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                          – Adam
                          May 11 '16 at 13:01






                        • 1




                          @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                          – RobP
                          Dec 13 '16 at 14:53















                        up vote
                        97
                        down vote



                        accepted







                        up vote
                        97
                        down vote



                        accepted






                        The problem lies in:



                        $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
                        $query->bind_param('s', $setting);


                        The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?



                        Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)






                        share|improve this answer














                        The problem lies in:



                        $query = $this->db->conn->prepare('SELECT value, param FROM ws_settings WHERE name = ?');
                        $query->bind_param('s', $setting);


                        The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT or WHERE clause) are not correct?



                        Also, consider use of something like $this->db->conn->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Apr 1 '17 at 2:20









                        miken32

                        22.8k84671




                        22.8k84671










                        answered Dec 10 '14 at 6:43









                        RobP

                        5,97421532




                        5,97421532








                        • 2




                          Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                          – Adam
                          May 11 '16 at 13:01






                        • 1




                          @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                          – RobP
                          Dec 13 '16 at 14:53
















                        • 2




                          Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                          – Adam
                          May 11 '16 at 13:01






                        • 1




                          @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                          – RobP
                          Dec 13 '16 at 14:53










                        2




                        2




                        Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                        – Adam
                        May 11 '16 at 13:01




                        Why do you suggest to call $query->error_listif the prepare methods returns false (and thus $query would be false?) when an error appears?
                        – Adam
                        May 11 '16 at 13:01




                        1




                        1




                        @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                        – RobP
                        Dec 13 '16 at 14:53






                        @Adam you have a point. Calling $query->error_list makes sense if the bind_param or an execute fails, but if $query is false then one should call error or error_list on the mysqli object, i.e. $this->db->conn->error_list.
                        – RobP
                        Dec 13 '16 at 14:53














                        up vote
                        17
                        down vote













                        Any time you get the...




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




                        ...it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!



                        First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:



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


                        If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.



                        Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();


                        The error is generic and not very helpful to you in solving what is going on.



                        With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();
                        // any additional code you need would go here.
                        } else {
                        $error = $mysqli->errno . ' ' . $mysqli->error;
                        echo $error; // 1054 Unknown column 'foo' in 'field list'
                        }


                        If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.



                        If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.






                        share|improve this answer

















                        • 4




                          Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                          – Dawid Ferenczy
                          Nov 27 '15 at 17:04















                        up vote
                        17
                        down vote













                        Any time you get the...




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




                        ...it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!



                        First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:



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


                        If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.



                        Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();


                        The error is generic and not very helpful to you in solving what is going on.



                        With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();
                        // any additional code you need would go here.
                        } else {
                        $error = $mysqli->errno . ' ' . $mysqli->error;
                        echo $error; // 1054 Unknown column 'foo' in 'field list'
                        }


                        If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.



                        If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.






                        share|improve this answer

















                        • 4




                          Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                          – Dawid Ferenczy
                          Nov 27 '15 at 17:04













                        up vote
                        17
                        down vote










                        up vote
                        17
                        down vote









                        Any time you get the...




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




                        ...it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!



                        First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:



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


                        If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.



                        Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();


                        The error is generic and not very helpful to you in solving what is going on.



                        With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();
                        // any additional code you need would go here.
                        } else {
                        $error = $mysqli->errno . ' ' . $mysqli->error;
                        echo $error; // 1054 Unknown column 'foo' in 'field list'
                        }


                        If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.



                        If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.






                        share|improve this answer












                        Any time you get the...




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




                        ...it is likely because there is an issue with your query. The prepare() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!



                        First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening <?php tag:



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


                        If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.



                        Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();


                        The error is generic and not very helpful to you in solving what is going on.



                        With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.



                        $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
                        if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
                        $query->bind_param('s', $definition);
                        $query->execute();
                        // any additional code you need would go here.
                        } else {
                        $error = $mysqli->errno . ' ' . $mysqli->error;
                        echo $error; // 1054 Unknown column 'foo' in 'field list'
                        }


                        If something is wrong you can spit out an error message which takes you directly to the issue. In this case there is no foo column in the table, solving the problem is trivial.



                        If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 5 '15 at 21:01









                        Jay Blanchard

                        35.3k125495




                        35.3k125495








                        • 4




                          Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                          – Dawid Ferenczy
                          Nov 27 '15 at 17:04














                        • 4




                          Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                          – Dawid Ferenczy
                          Nov 27 '15 at 17:04








                        4




                        4




                        Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                        – Dawid Ferenczy
                        Nov 27 '15 at 17:04




                        Very good answer mainly for advices about the error reporting. Programming with the error reporting disabled is like playing chess with eyes shut. People also don't know how to use a debugger (or that it even does exist).
                        – Dawid Ferenczy
                        Nov 27 '15 at 17:04










                        up vote
                        11
                        down vote













                        prepare return a boolean only when it fails therefore FALSE, to avoid the error you need to check if it is True first before executing:



                        $sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
                        if($query = $this->db->conn->prepare($sql)){
                        $query->bind_param('s', $setting);
                        $query->execute();
                        //rest of code here
                        }else{
                        //error !! don't go further
                        var_dump($this->db->error);
                        }





                        share|improve this answer

























                          up vote
                          11
                          down vote













                          prepare return a boolean only when it fails therefore FALSE, to avoid the error you need to check if it is True first before executing:



                          $sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
                          if($query = $this->db->conn->prepare($sql)){
                          $query->bind_param('s', $setting);
                          $query->execute();
                          //rest of code here
                          }else{
                          //error !! don't go further
                          var_dump($this->db->error);
                          }





                          share|improve this answer























                            up vote
                            11
                            down vote










                            up vote
                            11
                            down vote









                            prepare return a boolean only when it fails therefore FALSE, to avoid the error you need to check if it is True first before executing:



                            $sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
                            if($query = $this->db->conn->prepare($sql)){
                            $query->bind_param('s', $setting);
                            $query->execute();
                            //rest of code here
                            }else{
                            //error !! don't go further
                            var_dump($this->db->error);
                            }





                            share|improve this answer












                            prepare return a boolean only when it fails therefore FALSE, to avoid the error you need to check if it is True first before executing:



                            $sql = 'SELECT value, param FROM ws_settings WHERE name = ?';
                            if($query = $this->db->conn->prepare($sql)){
                            $query->bind_param('s', $setting);
                            $query->execute();
                            //rest of code here
                            }else{
                            //error !! don't go further
                            var_dump($this->db->error);
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 10 '14 at 6:48









                            meda

                            39.1k1166109




                            39.1k1166109






















                                up vote
                                6
                                down vote













                                Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
                                Always close your previous statement with



                                $statement->close();


                                If the syntax is correct, the following query will run well too.






                                share|improve this answer





















                                • This answer helped me.
                                  – Amine
                                  Jan 4 at 17:23















                                up vote
                                6
                                down vote













                                Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
                                Always close your previous statement with



                                $statement->close();


                                If the syntax is correct, the following query will run well too.






                                share|improve this answer





















                                • This answer helped me.
                                  – Amine
                                  Jan 4 at 17:23













                                up vote
                                6
                                down vote










                                up vote
                                6
                                down vote









                                Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
                                Always close your previous statement with



                                $statement->close();


                                If the syntax is correct, the following query will run well too.






                                share|improve this answer












                                Even when the query syntax is correct, prepare could return false, if there was a previous statement and it wasn't closed.
                                Always close your previous statement with



                                $statement->close();


                                If the syntax is correct, the following query will run well too.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 24 '17 at 8:08









                                Tamas Kalman

                                1,25411523




                                1,25411523












                                • This answer helped me.
                                  – Amine
                                  Jan 4 at 17:23


















                                • This answer helped me.
                                  – Amine
                                  Jan 4 at 17:23
















                                This answer helped me.
                                – Amine
                                Jan 4 at 17:23




                                This answer helped me.
                                – Amine
                                Jan 4 at 17:23










                                up vote
                                1
                                down vote













                                Another situation that can cause this problem is incorrect casting in your queries.



                                I know it may sound obvious, but I have run into this by using tablename instead of Tablename. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.






                                share|improve this answer



























                                  up vote
                                  1
                                  down vote













                                  Another situation that can cause this problem is incorrect casting in your queries.



                                  I know it may sound obvious, but I have run into this by using tablename instead of Tablename. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    Another situation that can cause this problem is incorrect casting in your queries.



                                    I know it may sound obvious, but I have run into this by using tablename instead of Tablename. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.






                                    share|improve this answer














                                    Another situation that can cause this problem is incorrect casting in your queries.



                                    I know it may sound obvious, but I have run into this by using tablename instead of Tablename. Check your queries, and make sure that you're using the same case as the actual names of the columns in your table.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 22 '17 at 13:54









                                    Cody Gray

                                    190k34370459




                                    190k34370459










                                    answered Sep 22 '17 at 12:04









                                    4ndyG

                                    307




                                    307






















                                        up vote
                                        0
                                        down vote













                                        Sometimes, it is also because of a wrong table name or column name in the prepare statement.



                                        See this:



                                        https://www.youtube.com/watch?v=LGHdQ1bBho8






                                        share|improve this answer

























                                          up vote
                                          0
                                          down vote













                                          Sometimes, it is also because of a wrong table name or column name in the prepare statement.



                                          See this:



                                          https://www.youtube.com/watch?v=LGHdQ1bBho8






                                          share|improve this answer























                                            up vote
                                            0
                                            down vote










                                            up vote
                                            0
                                            down vote









                                            Sometimes, it is also because of a wrong table name or column name in the prepare statement.



                                            See this:



                                            https://www.youtube.com/watch?v=LGHdQ1bBho8






                                            share|improve this answer












                                            Sometimes, it is also because of a wrong table name or column name in the prepare statement.



                                            See this:



                                            https://www.youtube.com/watch?v=LGHdQ1bBho8







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Feb 17 at 11:40









                                            Reejesh PK

                                            7127




                                            7127






















                                                up vote
                                                0
                                                down vote













                                                You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.






                                                share|improve this answer

























                                                  up vote
                                                  0
                                                  down vote













                                                  You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.






                                                  share|improve this answer























                                                    up vote
                                                    0
                                                    down vote










                                                    up vote
                                                    0
                                                    down vote









                                                    You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.






                                                    share|improve this answer












                                                    You should always try as much as possible to always put your statements in a try catch block ... it will always help in situations like this and will let you know whats wrong. Perhaps the table name or column name is wrong.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Mar 27 at 14:49









                                                    Sam Banana

                                                    1918




                                                    1918






















                                                        up vote
                                                        0
                                                        down vote













                                                        This particular error has very little to do with the actual error. Here is my similar experience and the solution...
                                                        I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...






                                                        share|improve this answer

























                                                          up vote
                                                          0
                                                          down vote













                                                          This particular error has very little to do with the actual error. Here is my similar experience and the solution...
                                                          I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...






                                                          share|improve this answer























                                                            up vote
                                                            0
                                                            down vote










                                                            up vote
                                                            0
                                                            down vote









                                                            This particular error has very little to do with the actual error. Here is my similar experience and the solution...
                                                            I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...






                                                            share|improve this answer












                                                            This particular error has very little to do with the actual error. Here is my similar experience and the solution...
                                                            I had a table that I use in my statement with "|database-name|.login" composite name. I thought this wouldn't be a problem. It was the problem indeed. Enclosing it inside square brackets solved my problem ("[|database-name|].[login]"). So, the problem is MySQL preserved words (other way around ;) )... make sure your columns too are not failing to this type of error scdenario...







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Mar 31 at 7:40









                                                            Sam Saarian

                                                            317212




                                                            317212






















                                                                up vote
                                                                0
                                                                down vote













                                                                Following two are the most probable cause of this issue:




                                                                1. Spelling mistake either in column names or table name

                                                                2. Previously established statement is not closed. Just close it before making the prepared statement.




                                                                $stmt->close(); // <<<-----This fixed the issue for me

                                                                $stmt = $conn->prepare("Insert statement");





                                                                share|improve this answer

























                                                                  up vote
                                                                  0
                                                                  down vote













                                                                  Following two are the most probable cause of this issue:




                                                                  1. Spelling mistake either in column names or table name

                                                                  2. Previously established statement is not closed. Just close it before making the prepared statement.




                                                                  $stmt->close(); // <<<-----This fixed the issue for me

                                                                  $stmt = $conn->prepare("Insert statement");





                                                                  share|improve this answer























                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    up vote
                                                                    0
                                                                    down vote









                                                                    Following two are the most probable cause of this issue:




                                                                    1. Spelling mistake either in column names or table name

                                                                    2. Previously established statement is not closed. Just close it before making the prepared statement.




                                                                    $stmt->close(); // <<<-----This fixed the issue for me

                                                                    $stmt = $conn->prepare("Insert statement");





                                                                    share|improve this answer












                                                                    Following two are the most probable cause of this issue:




                                                                    1. Spelling mistake either in column names or table name

                                                                    2. Previously established statement is not closed. Just close it before making the prepared statement.




                                                                    $stmt->close(); // <<<-----This fixed the issue for me

                                                                    $stmt = $conn->prepare("Insert statement");






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered May 1 at 19:10









                                                                    Hari Das

                                                                    2,96832535




                                                                    2,96832535






















                                                                        up vote
                                                                        0
                                                                        down vote













                                                                        Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
                                                                        INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?) may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)






                                                                        share|improve this answer



























                                                                          up vote
                                                                          0
                                                                          down vote













                                                                          Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
                                                                          INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?) may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)






                                                                          share|improve this answer

























                                                                            up vote
                                                                            0
                                                                            down vote










                                                                            up vote
                                                                            0
                                                                            down vote









                                                                            Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
                                                                            INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?) may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)






                                                                            share|improve this answer














                                                                            Sometimes explicitly stating your table column names (especially in an insert query) may help. For example, the query:
                                                                            INSERT INTO tableName(param1,param2,param3) VALUES(?,?,?) may work better as opposed to:INSERT INTO tableName VALUES(?,?,?)







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited May 20 at 18:06









                                                                            Lab Lab

                                                                            346321




                                                                            346321










                                                                            answered May 20 at 17:32









                                                                            ebite Zion

                                                                            508




                                                                            508






























                                                                                draft saved

                                                                                draft discarded




















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.





                                                                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                Please pay close attention to the following guidance:


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function () {
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27394710%2ffatal-error-call-to-a-member-function-bind-param-on-boolean%23new-answer', 'question_page');
                                                                                }
                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Create new schema in PostgreSQL using DBeaver

                                                                                Deepest pit of an array with Javascript: test on Codility

                                                                                Costa Masnaga