-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

只是不要将 mysqli 与它的 bind_result 一起使用,这确实会让您要求其他人计算您的变量。

要么使用 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 回答