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()
Error: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
The erro says that you have bind N variables but none fields, because it's an insert
does not return value. bind_result()
should use in selects statements.
To fix, remove this line:
$result=$stmt->bind_result($uuid, $name, $email, $encrypted_password, $salt);
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement
Replace '$ID'
in the query with ?
. The question mark is used to mark variables that need to be bound.
Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in
The problem is at select you are selecting more than 2 columns and only bind 2 in mysqli_stmt_bind_result
, in other words the numbers of columns in select must be the same number of variables in mysqli_stmt_bind_result
To fix, change
SELECT * from admins WHERE user_email=? AND user_pass=?
To:
SELECT user_mail, user_pass from admins WHERE user_email=? AND user_pass=?"
Related Topics
Yii2 Global Filter/Behavior to Force User to Authenticate First
Php, Failed Connecting to Database
What Http Status Code Is Supposed to Be Used to Tell the Client the Session Has Timed Out
PHP & MySQL: Using Group by for Categories
Tracking Email with PHP and Image
Using Comparison Operators in a PHP 'Switch' Statement
Laravel 5.1 Unknown Database Type Enum Requested
Laravel 5.1 Unknown Database Type Enum Requested
Location for Session Files in Apache/Php
JSON_Encode Produce JSON_Error_Utf8 from Mssql-Select
Send Post Data via Raw JSON with Postman
Check If a "Run-Time" Multidimensional Array Key Exists
Laravel 5.2 - Pluck() Method Returns Array