By mcbeav


2010-12-20 08:39:32 8 Comments

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.

6 comments

@Maciel Bombonato 2016-08-28 03:46:15

I am trying to help other people with little experience in PHP like me.

In my case, this error occurred because I had an SQL syntax error. The console stack trace did not show the problem.

When I fixed the SQL, the error was gone.

@Stefan 2011-06-14 08:58:49

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.

@Ripa Saha 2014-04-09 05:32:23

perfect information.

@ankush981 2014-12-27 17:20:16

Thank you! That helped me a lot. :)

@itsites 2016-05-30 19:51:20

Check the permissions of the user in database. User without "insert" permission causes "Call to a member function bind_param() on a non-object" message error too, when trying to insert.

@PHP Hater 2014-04-13 02:37:56

It appears that prepare is quite dumb. It doesn't rely query entirely into the MySQL side, by this, I mean, if in your query, you have a table that happens to have the same name of a keyword, say "user", "order", ..., it just doesn't recognize it as a table, but rather as what the keyword commands actually do, so the query turns out to be a mess and the prepare just fail.

To fix this is simple, you have to type it in the "correct" way adding "`" in both sides of the table name. Example:

`user`, `order`, `...`

It's correct, yet, I find it silly from prepare to have this behavior.

@fvdz 2012-07-18 09:40:58

i'm using the mysqli approach as well and got the same error when I created another instance of mysqli before closing the first instance. So its important to use close() before starting the same piece of code. For example:

$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);
$qSelect->close();  // <--- use close before calling the same function( wich contains $DBH code) again;

@qdinar 2016-08-24 19:50:07

and if he uses such function many times, i think, he should use reset() instead of close(), and move prepare() outside of funcion, for example, into constructor, an put statement into property like this: $this->qSelect .

@oezi 2010-12-20 08:48:57

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.

@mcbeav 2010-12-20 09:13:21

yeah its qSelect is returning bool(false) but this doesnt make any sense because i have a function that is practically the same and it works fine.

@Tom McGee 2017-01-30 21:41:07

Hm. I was also in the situation of having a function that is practically the same. Turns out, I had copied-and-pasted it in order to add a field. But I forgot the comma before the new field! Stupid mistake, but it just goes to show that it might just be a typing error on your part.

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

35 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 212581 View
  • 2439 Score
  • 15 Answer
  • Tags:   php mysql database

32 Answered Questions

1 Answered Questions

[SOLVED] Call to a member function bind_param() on boolean

  • 2016-05-02 12:20:50
  • RD7
  • 12010 View
  • 1 Score
  • 1 Answer
  • Tags:   php mysqli

3 Answered Questions

Sponsored Content