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']
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in
Your problem is with this line
$stmt->bind_result($bind, $shipping_name,$shipping_address1,$shipping_address12, ....);
You're trying to bind the variable-types, like you do with bind_param()
, which is wrong - because this function does not have a parameter like that. bind_result()
s only arguments are the values you select from the query, nothing else.
The solution is to simply remove $bind
from your bind_result()
call, making it
$stmt->bind_result($shipping_name, $shipping_address1, $shipping_address12, ....);
Reference
- http://php.net/manual/en/mysqli-stmt.bind-result.php
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);
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
- 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. - 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()
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);
Related Topics
Calling Closure Assigned to Object Property Directly
PHP Sort a Multidimensional Array by Element Containing Y-M-D H:I:S Date
Why I Have to Call 'Exit' After Redirection Through Header('Location..') in PHP
How to Convert an Object to an Array
PHP How to Find the Time Elapsed Since a Date Time
How to Debug in Woocommerce 3+
Using Str_Replace So That It Only Acts on the First Match
How to Generate in PHP All Combinations of Items in Multiple Arrays
How to Use Ajax in a Wordpress Shortcode
Facebook Graph API Not Work from 2.2 to 2.3
PHP Array Delete by Value (Not Key)
Filter Array by Its Keys Using an Array of Allowed Keys
How to Prevent Browser Cache For PHP Site
Remove Non-Utf8 Characters from String