Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    MySQLi Prepared Statements

    I'm getting the following error:
    Fatal error: Call to a member function bind_param() on a non-object in [edited out]security.php on line 86
    My relevant code is:
    PHP Code:
          $mysqli = new mysqli($this->dbHost$this->dbUser$this->dbPass$this->dbSchema); 
          if (!
    mysqli_connect_errno()) {
            
    $stmt $mysqli->prepare("SELECT g.groupname FROM tblgroupmembers as gm LEFT JOIN tblusergroups as g ON (g.id = gm.groupid) WHERE (gm.user=?)");
              
    $stmt->bind_param('s'$this->userName);
              
    $stmt->execute();
              
    $stmt->bind_result($groupNameFromDB);

              
    // do stuff with result
              
    $stmt->close();
          }
          
    $mysqli->close(); 
    Specifically, the error is referring to this part (line 86):
    PHP Code:
    $stmt->bind_param('s'$this->userName); 
    And stating that $stmt is a non-object, despite having been set above by:
    PHP Code:
    $stmt $mysqli->prepare("SELECT g.groupname FROM tblgroupmembers as gm LEFT JOIN tblusergroups as g ON (g.id = gm.groupid) WHERE (gm.user=?)"); 
    I've run that query (with the "?" replaced with a single-quoted username) in MySQL Query Browser without error. However running the script with var_dump($stmt) right here (after the assignment of $stmt to the result of prepare()) shows $stmt to be a bool of value false.

    So, prepare() is failing, returning false, and I'm trying to call a method of a Boolean variable. Bad. I get that. But what am I doing wrong with my prepare() call, as the SQL works in the Query Browser, and I've verified that mysqli_connect_errno() is false?

    According to http://us.php.net/manual/en/mysqli.prepare.php:
    PHP Code:
    $stmt $mysqli->prepare("SELECT District FROM City WHERE Name=?"
    Should work...
    Last edited by DDaku; 09-15-2009 at 03:45 PM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    You will notice in the examples in the manual that the $stmt = $mysqli->prepare() is enclosed in an if() {} conditional test so that the remainder of the code that is dependent on $stmt is only executed when the prepare() is successful.

    There is likely an error in the query statement. The query is parsed at the time of the prepare() statement. Use $mysqli->error to find out what error occurred -
    http://us.php.net/manual/en/mysqli.error.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Sorry, I'm an idiot. Found my problem. Using the wrong schema on the database

    CFMaBiSmAd, thanks for the response anyway, and yeah I had everything wrapped in IF checks, but removed that for debugging (an error is much nicer sometimes than a graceful failure.)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •