0

I am relatively new to PHP and am working on learning binding. I am getting a bind error:

mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

if( isset( $_GET['last_name'],$_GET['id'] )) {
    $last_name = trim($_GET['last_name']);
    $id = trim($_GET['id']);
    $people = $db->prepare( "select firstName, last_name, id from people where last_name = ? or id >= ?");
    $people->bind_param('ssi', $first_name, $last, $id);
    $people->execute();
    $people->bind_result( $first_name, $last, $id );

I'm getting the error on the bind_param line. I have 'ssi" which I though meant string, string, integer, and I have three variables. In my select, I have three correctly named fields. If I change to "si" and remove either name field, it works fine. so I am puzzled why adding a second string does not work.

Dharman
  • 30,962
  • 25
  • 85
  • 135
DVDmmike
  • 73
  • 1
  • 4

1 Answers1

0

You have two question marks in that clause:

"select firstName, last_name, id from people where last_name = ? or id >= ?"

(Pay attention to camelCases firstName param and underscored rest of params, it is good practice to stick with one naming convention to avoid problems)

which means You want to bind 2 params, but in next line You are trying to put 3 params

$people->bind_param('ssi', $first_name, $last_name, $id)

Delete $first_name variable from bind_param and 's' from types.

This should look like this:

$people->bind_param('si', $last_name, $id)

NOTE: Your $last and $first_name variables are undefined.

szymanskilukasz
  • 463
  • 3
  • 13
  • I agree about the naming conventions but these are the correct field names in the DB. I'm still not sure that I understand why this is not working. Doesn't my select call for three fields being selected? Why does the where clause retrieving two fields have anything to do with the number of fields that I want to bind? Perhaps, I just don't understand what binding is exactly. Why can't my where clause have any number of questions marks and assign (bind) any number of fields? – DVDmmike Jun 28 '14 at 15:56
  • "select firstName, last_name, id from people where last_name = ? or id >= ?" You're **selecting 3 fields** - firstName, last_name, id from table people with **2 conditions**: lasnt_name = ? or id >= ? since You're not specify those 2 conditions just place question marks as a placeholder You must bind 2 params to these placeholders. You must always bind equal number of variables to number of '?' in statement.Where clause not retrieves 2 fields. You always retrieve 3 fields with that statement but filtered by these 2 conditions which You must bind cause You dont specify it. – szymanskilukasz Jun 28 '14 at 18:25
  • You must always bind equal number of variables to number of '?' in statement.Where clause not retrieves 2 fields. I guess what I'm trying to do was different that what binding really is, hence, I don't understand binding properly. What I was going to do later was a while loop to retrieve three fields even though I was only retrieving data points from the user. While( $people->fetch() ) So I guess that I have to change my binding select to be only two as you have state and change my while loop to use a new select criteria instead. – DVDmmike Jun 29 '14 at 15:23