1

I'm learning mysqli_. If I run the query without the bind_param it works, but if I add the bind_param my query stops working.

This is my code:

// Make a connection to database.
$user = 'test';
$sql = "SELECT * FROM `user` WHERE `user` = ?";
$querySelect = $mysqli->prepare($sql);
$querySelect->bind_param('s', $user);
$querySelect->execute();
echo 'N: '.$querySelect->num_rows.'<br>'; // Got 0, but the correct result is 1.

ERROR:

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

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
Andrea php
  • 393
  • 4
  • 21
  • 3
    Errors, please. "Not working" is not a useful bit of information. You may want to [enable exceptions](http://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) to have errors bubbled up. – tadman Jul 13 '16 at 17:55
  • 1
    That call isn't shown in this code, so you'll have to track it down. – tadman Jul 13 '16 at 17:56
  • You should assign the execute method to a variable and work with this – JRsz Jul 13 '16 at 17:56
  • Error added in question – Andrea php Jul 13 '16 at 17:57
  • Add `var_dump($querySelect);` before the call to `bind_param`. My guess is that the `prepare` statement is failing. Per the [docs](http://php.net/manual/en/mysqli.prepare.php): _"`mysqli_prepare()` returns a statement object or `FALSE` if an error occurred."_ – War10ck Jul 13 '16 at 17:59
  • Got `object(mysqli_stmt)#2 (0) { }` i'm on php 5.2 – Andrea php Jul 13 '16 at 18:00
  • When you are using PDO you should use `$querySelect->bindParam('s', $user);`. – Hamid Jul 13 '16 at 18:03
  • i'm using `mysqli` – Andrea php Jul 13 '16 at 18:05

1 Answers1

2

I guess this line returns false:

$querySelect = $mysqli->prepare($sql);

try to do:

var_dump($querySelect);

in order to be sure. If return false, that means something wrong with getting data user from database (wrong table, connection, table column, ...)

but this sql is also strange:

$sql = "SELECT * FROM `user` WHERE `user` = ?";

maybe you wanted to write:

$sql = "SELECT * FROM `user` WHERE `user_id` = ?";

so, user_id instead of user or maybe only id, depends on the name of you primary key

Marko Krstic
  • 1,417
  • 1
  • 11
  • 13