【问题标题】:mysqli_stmt_bind_result() number of variables doesnt match? help me how to count [duplicate]mysqli_stmt_bind_result() 变量数不匹配?帮我计算​​一下[重复]
【发布时间】:2013-03-24 03:11:52
【问题描述】:

我只是想从数据库中选择一堆字段 - 就像我以前做过很多次一样......但不知何故我得到了这个错误:

警告: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);        
}

谁能看出问题所在?

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    只是不要将 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() 从查询中获取熟悉的数组,而不需要手动绑定每个变量,尽管不能保证它有效。

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      相关资源
      最近更新 更多