I've just started learning MYSQLI prepared statements technique and faced really annoying issue. My code stops at bind_result(). And I just don't get it what could be wrong, so please, guys, help me.
Here is my config.php:
<?php
$db_name = 'site';
$db_user = 'root';
$db_pass = 'root';
$db_host = 'localhost';
$db_port = 8889;
$db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
And my main code:
<?php
include 'config.php';
$post_id=450;
$stmt = $db->prepare("SELECT * FROM messages WHERE post_id = ?");
if ( false===$stmt ) {
die('prepare() failed: ' . htmlspecialchars($db->error));
}
$ex=$stmt->bind_param('i', $post_id); // Bind "$post_id" to parameter.
if ( false===$ex ) {
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$ex=$stmt->execute();// Execute the prepared query.
if ( false===$ex ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
$ex=$stmt->store_result();
if ( false===$ex ) {
die('store_result() failed: ' . htmlspecialchars($stmt->error));
}
$ex=$stmt->bind_result($p);
if ( false===$ex ) {
die('bind_result() failed: ' . htmlspecialchars($stmt->error));
}
while($stmt->fetch()){
echo $p;
}
?>
Here is how your config.php have to be
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); // change it to 0 on production server
$db_name = 'site';
$db_user = 'root';
$db_pass = 'root';
$db_host = 'localhost';
$db_port = 8889;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
and here is how your main code have to be
<?php
include 'config.php';
$post_id=450;
$stmt = $db->prepare("SELECT * FROM messages WHERE post_id = ?");
$stmt->bind_param('i', $post_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($p); // here is where an error have to be shown
while($stmt->fetch()){
echo $p;
}
Your initial problem is that you weren't checking every call for failure, just the few at the start. Both store_result
and bind_result
can also fail.
Your specific problem (once you've fixed the initial one) is that you're doing select *
to get multiple columns but you're only binding one in the result. That's evident from the error message you get from the bind_result
call:
Number of bind variables doesn't match number of fields
If you change the select
statement to something like select id from ...
, you'll find it starts working.
However, assuming you want more columns, you should provide more variables in the binding, something like:
$stmt = $db->prepare("SELECT col1, col2 FROM messages WHERE post_id = ?");
:
$ex = $stmt->bind_result($param1, $param2);
You'll notice I'm not using select *
up there, it's better to use explicit column naming in almost all situations so that:
Pretty much the only place I've ever found it's okay to use select *
is in DB analysis tools where you want to get everything. If you know what you want in advance, that's what you should ask for.
Make sure that your error reporting is turned on by adding the following code at the top of your page before include 'config.php' :
error_reporting(E_ALL);
then you can diagnose it to find out where is the error comming from.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.