Insert - Number of Bind Variables Doesn't Match Number of Fields in Prepared Statement

bind_result() - Number of variables doesn't match number of fields

You are not using bind_result() properly.

Binds columns in the result set to variables.

You are trying to bind the entire result set into a single variable.
You need to provide a variable for each column in the result set.

$stmt->bind_result($topic,$detail,$email,$name,$datetime);

Here is where it fits in:

$sql="SELECT `topic`,`detail`,`email`,`name`,`datetime` FROM `$safe_tbl_name` WHERE id=?";
if($stmt=$con->prepare($sql)){
$stmt->bind_param("s",$id);
$stmt->execute();
$stmt->bind_result($topic,$detail,$email,$name,$datetime);
//while($stmt->fetch()){ not wrong, but not necessary to loop if only one row
$stmt->fetch();
echo "<table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">";
echo "<tr>";
echo "<td>";
echo "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"1\" bgcolor=\"#FFFFFF\">";
echo "<tr>";
echo "<td bgcolor=\"#F8F7F1\"><strong>$topic</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=\"#F8F7F1\">$detail</td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=\"#F8F7F1\"><strong>By :</strong>$name<strong>Email : </strong>$email</td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=\"#F8F7F1\"><strong>Date/time : </strong>$datetime</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
//}
$stmt->close();
}

Alternatively, if you want to use the * in your SELECT, you could try the following non-bind_result method. (all examples that I have read online only use bind_result when not using * in the SELECT.

if($stmt->execute()){
$result=$stmt->get_result();
$rows[]=$result->fetch_assoc();
}else{
echo "execute failed"; // but I don't think this is your problem
}
// $rows['topic']
// $rows['detail']
// $rows['email']
// $rows['name']
// $rows['datetime']

Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement

The problem is that the number of bind variables doesn't match number of fields in prepared statement. You either need to bind fewer variables or you need to add the missing fourth column to your SQL

//                                VVV Add the fourth column here
$sql = "SELECT id, email, password, userLevel FROM users WHERE email = ? AND userLevel=?";

or bind fewer variables

//                                                         VVV Remove the fourth binding
mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password);

INSERT - Number of bind variables doesn't match number of fields in prepared statement

You are attempting to bind_result on a statement that is not returning any results.

Remove this line.
$stmt -> bind_result($user, $pw);

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

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

mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in

There are, as far as I can see, two issues here

  1. You're not binding your input - this is the whole point of prepared statements. You need mysqli_stmt_bind_param(). Injecting variables directly in the query is not safe, and is why we prepare our queries.
  2. You're not binding the exact amount of columns selected with mysqli_stmt_bind_result(). This needs to be the exact amount of columns you select.

To make this work, you need to use placeholders ? in the query instead of variables (and use mysqli_stmt_bind_param()). You should also select the specific columns, and not do SELECT *. If you do SELECT *, you can still make it work if you still bind the same amount of results - but the code will break if you add one column at a later time.

This code below has been altered to select the specific columns, and match that with as many results you bind (columns) in mysqli_stmt_bind_result(). A placeholder in the query has been added, and usage of mysqli_stmt_bind_param() implemented.

$query = "SELECT id, user_name, produc_id, image FROM cart WHERE user_name=?";

if ($stmt = mysqli_prepare($mysqli, $query)) {

mysqli_stmt_bind_param("s", $username);

/* execute statement */
mysqli_stmt_execute($stmt);

/* bind result variables */
mysqli_stmt_bind_result($stmt, $user_id, $username, $product_id, $image);

/* fetch values */
while ($row=mysqli_stmt_fetch($stmt)) {
// Fetch here
}
}
  • mysqli_stmt::bind_param()
  • mysqli_stmt::bind_result()

Number of bind variables doesn't match number of fields in prepared statement when changing a password

Update query only return true or false it will not return updated data. So change you code like below:

<?php
$con = mysqli_connect("localhost", "id2815222_bigbrother", "orwell", "id2815222_orwell");

$studentno = $_POST["studentno"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "UPDATE tbl_users SET password = ? WHERE studentno = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
$res = mysqli_stmt_execute($statement); // store result in $res



$response = array();
$response["success"] = false;

if($res){ //check whether it is true or false
$response["success"] = true;
}

echo json_encode($response);
?>


Related Topics



Leave a reply



Submit