我准备了一个失败的准备好的声明......
$stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
//var_dump($stmt2);
if ( !$stmt2 || !empty($con->error)) {
die('Select forum posts prepare() failed: ' . htmlspecialchars($con->error));
}
我在我的第一份准备好的声明中几乎完全相同,并且它完成得很好。我不确定如果这导致问题或者情况如何,我是否有两个相互准备好的陈述?
现在我收到了这个错误......
- 选择论坛帖子prepare()失败: -
上述代码中的准备失败错误。
有没有人看到任何导致此失败的事情?
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
//Prepared SELECT stmt to get forum topics
$stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND id=? LIMIT 1");
if (!$stmt || $con->error ) {
die('Select topics prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
die('Select topics bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('Select topics execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
foreach($stmt as $row) {
//Prepared SELECT stmt to get forum posts
$stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
//var_dump($stmt2);
if ( !$stmt2 || !empty($con->error)) {
die('Select forum posts prepare() failed: ' . htmlspecialchars($con->error));
}
//var_dump($con->error);
if(!$stmt2->bind_param('ii', $cid, $tid)) {
die('Select forum posts bind_param() failed: ' . htmlspecialchars($stmt2->error));
}
if(!$stmt2->execute()) {
die('Select forum posts execute() failed: ' . htmlspecialchars($stmt2->error));
}
//while($row2 = mysqli_fetch_assoc($stmt2)){
foreach($stmt2 as $row2) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
}
} else {
echo "<p>This topic does not exist.</p>";
}
更新 - 可能的INNER JOIN,像这样吗?
//Prepared SELECT stmt to get forum topics
$stmt = $con->prepare("SELECT * FROM forum_topics AS f INNER JOIN forum_posts AS p ON
f.category_id=p.category_id ON f.id=p.topic_id WHERE s.category_id=? AND s.id=? LIMIT 1");
if (!$stmt || $con->error ) {
die('Select topics prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
die('Select topics bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('Select topics execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
foreach($stmt as $row) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
}
} else {
echo "<p>This topic does not exist.</p>";
}
}*/
/*$stmt = $con->prepare("SELECT topic_title, post_content FROM forum_topics, forum_posts
WHERE forum_topics.category_id=forum_posts.category_id
AND forum_topics.category_id=?");*/
//$stmt = $con->prepare("SELECT topic_title, topic_creator FROM forum_posts AS f LEFT JOIN forum_topics AS p ON f.category_id=p.category_id WHERE f.category_id=?");
$stmt = $con->prepare("SELECT topic_title, topic_creator FROM forum_posts AS p, forum_topics AS f WHERE f.category_id=? AND p.topic_id=? LIMIT 1");
if (!$stmt || $con->error ) {
die('Select forum prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
die('Select forum bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('Select forum execute() failed: ' . htmlspecialchars($stmt->error));
}
mysqli_stmt_bind_result($stmt, $post_content, $topic_title);
$stmt->store_result();
echo $stmt->store_result();
$numrows = $stmt->num_rows;
if($stmt->num_rows > 0){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
while (mysqli_stmt_fetch($stmt)) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$topic_title."<br />
by " . $post_content ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
mysqli_stmt_close($stmt);
} else {
echo "<p>This topic does not exist.</p>";
}
?>
</body>
</html>
答案 0 :(得分:2)
在OP中花了5个小时聊天,最后解决问题
问题;
没有必要;
解;
//bind the result
mysqli_stmt_bind_result($stmt, $topic_title, $topic_creator, $postDate, $postcontent);
//use while loop instead foreach
while (mysqli_stmt_fetch($stmt))
工作代码;
//Prepared SELECT stmt to get forum topics
$stmt = $con->prepare("SELECT topic_title, topic_creator, post_date, post_content FROM forum_posts AS p, forum_topics AS f WHERE f.category_id=? AND p.topic_id=?");
if (!$stmt || $con->error ) {
die('Select forum prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
die('Select forum bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('Select forum execute() failed: ' . htmlspecialchars($stmt->error));
}
mysqli_stmt_bind_result($stmt, $topic_title, $topic_creator, $postDate, $postcontent);
$stmt->store_result();
if($stmt->num_rows > 0){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
while (mysqli_stmt_fetch($stmt)) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$topic_title."<br />
by ".$topic_creator." - " .$postDate. "<hr />" . $postcontent ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
mysqli_stmt_close($stmt);
} else {
echo "<p>This topic does not exist.</p>";
}
忘了提一下,这是我第一次使用mysqli,我读过它但从未尝试过。