-2

データベースから一連のフィールドを選択したいだけです-以前に何度も行ったことがあります...しかし、どういうわけかこのエラーが発生します:

警告: mysqli_stmt_bind_result(): バインド変数の数が準備済みステートメントのフィールド数と一致しません

しかし、正確に 14 列を数えているのに、14 個の変数を追加すると、なぜこのエラーがスローされるのでしょうか?

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);        
}

誰が何が悪いのか見ることができますか?

4

1 に答える 1

0

bind_result で mysqli を使用しないでください。これにより、実際に他の人に変数をカウントするように依頼することになります。

PDO を使用すると、コードが次のように短くなります。

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
}

または、少なくとも get_result() を使用して、すべての変数を手動でバインドする必要なく、クエリから使い慣れた配列を取得しますが、動作することは保証されていません。

于 2013-03-24T05:35:54.480 に答える