Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

//CALL FROM PAGE ONE check($username);  //FUNCTION ON PAGE 2 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.

like image 950
mcbeav Avatar asked Dec 20 '10 08:12

mcbeav


2 Answers

Well, one reason prepare() can fail is if the sql statement sent to it is not valid in the current DB.

prepare() will then return false.

Eg - if the table name is not correct or one or more field in the query does not exist.

like image 121
Stefan Avatar answered Sep 19 '22 13:09

Stefan


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.

like image 32
oezi Avatar answered Sep 22 '22 13:09

oezi