我刚开始学习 MYSQLI 准备语句技术,遇到了非常烦人的问题。我的代码在 bind_result() 处停止。而且我只是不明白可能出了什么问题,所以请伙计们帮助我。
这是我的 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 . ']');
}
?>
我的主要代码:
<?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;
}
?>