-2

I simply want to select a bunch of fields from a data base - as I have done it a lot of times before... But somehow I get this error:

Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement

But I count exactly 14 columns, so why when I add 14 variables does it throw this error?

public function get_invitation_fields()
{
  $this->fields_db = array();
  include('system/mysqli_db.php'); //db connection opens here
  $statement="SELECT 
  invitation_ID,
  recipient,
  text,
  name,
  usr_ID,
  deleted,
  send_date,
  resend_date,
  last_date,
  status,
  register_date,
  verify_date,
  redeem_date
  trans_ID 
  FROM invitations WHERE email=?";

  if ($stmt = mysqli_prepare($db, $statement))
  {
    mysqli_stmt_bind_param($stmt, "s", $this->email);

    if(!mysqli_stmt_execute($stmt))
    {echo mysqli_stmt_error($stmt); echo mysqli_error($db); }

    mysqli_stmt_bind_result($stmt,
    $this->fields_db['invitation_ID'],
    $this->fields_db['recipient'],
    $this->fields_db['text'],
    $this->fields_db['name'],
    $this->fields_db['usr_ID'],
    $this->fields_db['deleted'],
    $this->fields_db['send_date'],
    $this->fields_db['resend_date'],
    $this->fields_db['last_date'],
    $this->fields_db['status'],
    $this->fields_db['register_date'],
    $this->fields_db['verify_date'],
    $this->fields_db['redeem_date'],
    $this->fields_db['trans_ID']
    ); //PHP points the error to this line.

    mysqli_stmt_fetch($stmt);

    $this->invite_fields_db = $this->fields_db;

    mysqli_stmt_close($stmt);
  }
  else
  {
    echo mysqli_stmt_error($stmt);
    echo mysqli_error($db);
  }
  mysqli_close($db);        
}

Can anyone see what's wrong?

oliver_siegel
  • 1,666
  • 3
  • 22
  • 37

1 Answers1

0

Just don't use mysqli with it's bind_result, which indeed makes you ask other people to count your variables.

Either use PDO, which will make your code as short as

public function get_invitation_fields($email)
{
    global $pdo; // connection should be opened ONCE at the beginning of the whole app

    $sql = "SELECT * FROM invitations WHERE email=?";
    $stm = $pdo->prepare($sql);
    $stm->execute(array($email)); 
    return $stm->fetch(); // values have to be RETURNED, not assigned
}

or at least use get_result() to get a familiar array from the query, without need of binding every variable manually, though it's not guaranteed to work.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345