【问题标题】:How to prepare number of bind variables to match the number of fields in prepared statement [duplicate]如何准备绑定变量的数量以匹配准备好的语句中的字段数量[重复]
【发布时间】:2018-04-07 08:24:42
【问题描述】:

首先,我知道这是一个重复的问题,我问的是同一个问题。但是我已经阅读了与同一问题相关的所有解决方案,但是当我遵循建议的解决方案时,它会触发更多警告出现。 这就是我的代码中的内容

if($stmt = $mysqli->prepare("SELECT * FROM emergency WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();

$stmt->bind_result($id, $patient_seen_u, $patient_seen_a);
$stmt->fetch();

// show the form
renderForm($patient_seen_u, $patient_seen_a, NULL, $id);

$stmt->close();

获取

警告:mysqli_stmt::bind_result():绑定变量的数量不 匹配准备好的语句中的字段数 C:\xampp\htdocs\gsd\emergency\records.php 在第 122 行

所以我把代码改成

if($stmt = $mysqli->prepare("SELECT date, patient_seen_u, patient_seen_a FROM emergency WHERE id ='?'"))
{
$stmt->bind_param("i", $id);
$stmt->execute();

$stmt->bind_result($id, $date, $patient_seen_u, $patient_seen_a);
$stmt->fetch();

// show the form
renderForm($date, $patient_seen_u, $patient_seen_a ,NULL, $id);

$stmt->close();
}

只得到这些警告;

警告:mysqli_stmt::bind_param(): 变量数不匹配 准备好的语句中的参数数量 C:\xampp\htdocs\gsd\emergency\records.php 在第 119 行

警告:mysqli_stmt::bind_result():绑定变量的数量不 匹配准备好的语句中的字段数 C:\xampp\htdocs\gsd\emergency\records.php 在第 122 行

并尝试这样做;

if($stmt = $mysqli->prepare("SELECT `date`, `patient_seen_u`,`patient_seen_a`  FROM `emergency` WHERE `id` = '?'"))
{
$stmt->bind_param("iii", $id);
$stmt->execute();

$stmt->bind_result($id, $date, $patient_seen_u, $patient_seen_a);
$stmt->fetch();

// show the form
renderForm($date, $patient_seen_u, $patient_seen_a ,NULL, $id);

$stmt->close();
}

但代码也不起作用。我该如何解决这个问题?

【问题讨论】:

  • 那么 $id 是什么?
  • 它用于从 URL 中获取 id $id = $_GET['id'];

标签: php mysqli prepared-statement bindparam


【解决方案1】:

删除占位符周围的引号并添加 id 列以匹配 bind_result 的排列顺序

if($stmt = $mysqli->prepare("SELECT id, date, patient_seen_u, patient_seen_a FROM emergency WHERE id =?")) {

$stmt->bind_param("i", $id);// bind as integer 


$stmt->bind_result($id, $date, $patient_seen_u, $patient_seen_a);

【讨论】:

    【解决方案2】:
    if($stmt = $mysqli->prepare("SELECT id, date, patient_seen_u, patient_seen_a FROM emergency WHERE id =?")) 
    {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    
    $stmt->bind_result($id, $date, $patient_seen_u, $patient_seen_a);
    $stmt->fetch();
    
    // show the form
    renderForm($date, $patient_seen_u, $patient_seen_a ,NULL, $id);
    
    $stmt->close();
    }
    

    现在它工作正常。谢谢你。

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 2014-01-06
      • 2023-03-26
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      相关资源
      最近更新 更多