0

I'm trying to retrieve the most recent date out of my DB with this:

$stmt2 = $conn->prepare("SELECT reportDate FROM reports WHERE bot=? ORDER BY reportDate DESC LIMIT ?");
$limit = 1;
$stmt2->bind_param("ii", $id, $limit);
$stmt2->execute();
$stmt2->bind_result($lastSeen);

I get the error:

PHP Fatal error: Call to a member function bind_param() on a non-object

I know this usually happens when there's a syntax error in the SQL statement, but I've tried running it in phpmyadmin and it works like a charm. Any suggestion?

  • 3
    I dont think you can set the limit placeholder it has to be static value. – Abhik Chakraborty Mar 30 '15 at 13:53
  • 2
    You have not defined `$id`? But the error here means that `$stmt2` is not an object when you call `bind_param()`. – KIKO Software Mar 30 '15 at 13:55
  • I'm sorry i forgot to mention that $id is a variable that i'm fetching with while($stmt->fetch()) through another query (which works normally) – Nicolas Figini Mar 30 '15 at 13:57
  • 1
    don't try to guess what caused the error, turn on error reporting then, `$conn->prepare("query here") or die($conn->error);`. anyway, binding limit offset seems to be accepted http://stackoverflow.com/questions/5375182/php-mysqli-prepared-statement – Kevin Mar 30 '15 at 14:02

2 Answers2

2

LIMIT does not take any parameters from prepared statements ordinarily, hence your prepare fails. And that is why you see that error on the next call.

Also go through How to apply bindValue method in LIMIT clause? and LIMIT keyword on MySQL with prepared statement

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

$stmt2 is not an object, try to var_dump() it.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
fdglefevre
  • 672
  • 4
  • 15
  • This is what i get when i var_dump($stmt2): object(mysqli_stmt)#3 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2) ["field_count"]=> int(1) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(2) } – Nicolas Figini Mar 30 '15 at 14:30
  • `$stmt2->bind_param("ii", $id, $limit);` What's `ii` ? You should read the doc: http://php.net/manual/en/pdostatement.bindparam.php – fdglefevre Mar 30 '15 at 14:48
  • Oh sorry, I use PDO the most of the time and I forget the title of your question. :) So you don't have the Fatal error anymore no ? – fdglefevre Mar 30 '15 at 15:06
  • I'm still struggling with this query, but i've come to think that the problem might be related to the fact that i'm using multiple nested prepared statements. I've been trying with other queries and the result is the same :/ – Nicolas Figini Mar 30 '15 at 15:16
  • Hum, maybe your problem is you are using the same variables names? – fdglefevre Mar 30 '15 at 15:23