Asked  7 Months ago    Answers:  3   Viewed   29 times

I am trying to bind a variable in this prepared statement, but i keep receiving the error:

Call to a member function bind_param() on a non-object

The function is called, and variables are passed to it. When i change the function to just echo the variable, the variable prints on the page fine, but if i try to bind it here i receive the error. can anyone help?


function check($username){
$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);

i know the function is not completely written here, but that shouldn't be a problem. I don't understand why i am receiving this error.



as the error-message says, $qSelect seems to be not an object. try to debug this by using var_dump($qSelect); right after your prepare-call. also check if getDBH() returns what you need.

sounds like the prepare-call fails (don't know why) and so it returns false - false is not an object, so you can't call bind_param() on that.

EDIT: you havn't given the info, but it looks like you're using PHP's PDO. In that case, take a look at the documentation.

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

You should configure your server to return those PDO-Exceptions, which would tell you why the prepare call fails.

Tuesday, June 1, 2021
answered 7 Months ago

For anyone else searching for this solution, as bwewsing suggests I needed to add in store_result however from the links provided above it's not too clear where this should be implemented.

Implementation should take place in the initial calling method. So, as the get_content() method calls the get_template() method it should be implemented here in the following manner:

    private function get_content($pageId){
    $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
    $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
         while ($retreiveContent->fetch()) {
            //Variables will be populated for this row.
            //Update the tags in the template.
            $template = $this->get_template($template_id);
            $template = str_replace('[i1]',$i1,$template);
            $template = str_replace('[i2]',$i2,$template);
            //$theTemplate is populated with content. Probably want to echo here
            echo $template;
Wednesday, March 31, 2021
answered 9 Months ago

This means that the query inside mysqli::prepare() resulted in an error.

According to the doc:

mysqli_prepare() returns a statement object or FALSE if an error occurred.

Try to properly escape desc, which is a reserved keyword in MySQL ( is not proper escaping):

$stmt = $mysqli->prepare('UPDATE links SET title = ?, `desc` = ? WHERE id = ?');
Saturday, May 29, 2021
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :