PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to prepare number of bind variables to match the number of fields in prepared statement

 September 14, 2022     bindparam, mysqli, php, prepared-statement     No comments   

Issue

Firstly, I know that this is a repeating question and I'm asking the same question. But I have read all the solution provided that linked to the same problem, but when I followed the suggested solution, it will trigger more warnings to appear. This is what I have in my code

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

to get the

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\gsd\emergency\records.php on line 122

so I change the codes into

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

only to get these warning;

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\gsd\emergency\records.php on line 119

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\gsd\emergency\records.php on line 122

and also try to do this;

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

but the code is not working either. How can I solve this?


Solution

Remove the quotes surrounding the placeholder and also add the id column to match the arrangement order for 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);


Answered By - Rotimi
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing