4
$stmt = $mysqli->prepare('select Un from member where Lock = ? and Activated = ?');
$stmt -> bind_param("ss", 'N', 'Y');//This line gave the error
$stmt -> execute();
$stmt->store_result();//apply to prepare statement
$numRows = $stmt->num_rows;

if ($numRows > 0)//if have result
 while ($row = $stmt->fetch())

my above code gave me a "Call to a member function bind_param() on a non-object" error. I really don't get it why i getting this error. I have correct cols name.

I am new to mysqli and would like to learn how to debug such error.

  1. what is the problem with my prepare statement or bind_param()?
  2. Please teach me how to debug such error
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jerry
  • 41
  • 1
  • 1
  • 2
  • `bind_param()` expects variables passed by reference. You can't pass string literals by reference. – Dharman Jan 17 '20 at 23:17

3 Answers3

14

Call to a member function bind_param() on a non-object means that $stmt, which you're trying to call bind_param on, is not an object. Why is it not an object? Because $mysqli->prepare did not return an object. Why did it not return an object?

mysqli_prepare() returns a statement object or FALSE if an error occurred.
http://www.php.net/manual/en/mysqli.prepare.php

So that means an error must have occurred. You should turn on error_reporting, which will probably tell you, or examine $mysqli->error(), which may tell you as well.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Error turn on and it said " You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Lock = ? and Activated = ?'" I still can't see why my sql is wrong. – Jerry Jul 12 '11 at 06:11
  • 1
    @Jerry `Lock` is a [reserved keyword](http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html). Put your field names in backticks to avoid this ambiguity: `WHERE \`Lock\` = ...` – deceze Jul 12 '11 at 06:19
  • thanks =D But now having another problem that "Cannot pass parameter 2 by reference". To what i know, it refer to "Y"? What i done wrong now? – Jerry Jul 12 '11 at 06:33
  • 1
    @Jerry The parameters need to be *variables*, not just text. If it's just unchanging static text you don't need to bind the values in the first place, you can just write them into your query as-is. – deceze Jul 12 '11 at 06:46
2
$stmt->bind_param("ss", 'N', 'Y');//This line gave the error 

Here you need to sure about data type in database.

j0k
  • 22,600
  • 28
  • 79
  • 90
0

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

Source.

$mysqli->prepare() may be returning FALSE. Write a condition to deal with that situation.

alex
  • 479,566
  • 201
  • 878
  • 984