背靠背准备语句中第二个准备语句的问题
Issues with second prepared statement in back to back prepared statements
我正在尝试使以下准备好的语句起作用。我正在尝试建立一个论坛,我目前正在做的部分是从主题中获取帖子。
第一个查询有效,num rows 部分也有效。我能够看到 "Add Replay" 按钮。但是,一旦我进入第二个查询,它就开始变得有点粗糙,我无法弄清楚到底出了什么问题。我有以下错误..
Warning: mysqli::query() expects parameter 1 to be string, object given
这条线
if ($result2 = $con->query($stmt2)) {
我试图从我的第二个查询的准备行中删除 if 以提供帮助,但它什么也没做。
是什么破坏了它,如果是,我该如何解决。如果那不是什么错,那是什么?
$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
throw new Exception("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* activate reporting */
$driver = new mysqli_driver();
try {
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
echo $cid . "<br>";
echo $tid;
//Prepare
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {
$stmt->bind_param("ii", $cid, $tid);
$stmt->execute();
$stmt->bind_result($topic_id, $category_id, $topic_title, $topic_creator, $topic_last_user, $topic_date, $topic_reply_date, $topic_views);
//var_dump($stmt);
if (!$stmt) {
throw new Exception($con->error);
}
}
$stmt->store_result();
$numrows = $stmt->num_rows;
echo $numrows;
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>";
}
}
while ($row = $stmt->fetch()) {
//Prepared SELECT stmt to get forum posts
//if($stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?"))
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
//$result2 = $stmt2->get_result();
if (!$stmt2) {
throw new Exception($con->error);
}
}
}
if ($result2 = $con->query($stmt2)) {
while ($row2 = $result2->fetch_assoc() ) {
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>";
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
更新:当前代码-
while ($row = $stmt->fetch()) {
//Prepared SELECT stmt to get forum posts
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
//$result2 = $stmt2->get_result();
if (!$stmt2) {
throw new Exception($con->error);
}
}
}
$num_rows2 = $stmt2->num_rows;
echo $num_rows2;
//if ($result2 = $con->query($stmt2)) {
//if ($result2 = $con->query("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {
if($num_rows2) {
//while ($row2 = $result2->fetch() ) {
while($row2 = $stmt2->fetch()) {
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>";
}
替换:
while($row2 = $stmt2->fetch()) {
与:
while($stmt2->fetch()) {
然后使用以下方法访问值:
$post_id // not $row2['post_id']
bind_result() 函数直接设置这些变量,如 here 所述。
我正在尝试使以下准备好的语句起作用。我正在尝试建立一个论坛,我目前正在做的部分是从主题中获取帖子。
第一个查询有效,num rows 部分也有效。我能够看到 "Add Replay" 按钮。但是,一旦我进入第二个查询,它就开始变得有点粗糙,我无法弄清楚到底出了什么问题。我有以下错误..
Warning: mysqli::query() expects parameter 1 to be string, object given
这条线
if ($result2 = $con->query($stmt2)) {
我试图从我的第二个查询的准备行中删除 if 以提供帮助,但它什么也没做。
是什么破坏了它,如果是,我该如何解决。如果那不是什么错,那是什么?
$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
throw new Exception("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* activate reporting */
$driver = new mysqli_driver();
try {
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
echo $cid . "<br>";
echo $tid;
//Prepare
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {
$stmt->bind_param("ii", $cid, $tid);
$stmt->execute();
$stmt->bind_result($topic_id, $category_id, $topic_title, $topic_creator, $topic_last_user, $topic_date, $topic_reply_date, $topic_views);
//var_dump($stmt);
if (!$stmt) {
throw new Exception($con->error);
}
}
$stmt->store_result();
$numrows = $stmt->num_rows;
echo $numrows;
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>";
}
}
while ($row = $stmt->fetch()) {
//Prepared SELECT stmt to get forum posts
//if($stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?"))
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
//$result2 = $stmt2->get_result();
if (!$stmt2) {
throw new Exception($con->error);
}
}
}
if ($result2 = $con->query($stmt2)) {
while ($row2 = $result2->fetch_assoc() ) {
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>";
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
更新:当前代码-
while ($row = $stmt->fetch()) {
//Prepared SELECT stmt to get forum posts
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
//$result2 = $stmt2->get_result();
if (!$stmt2) {
throw new Exception($con->error);
}
}
}
$num_rows2 = $stmt2->num_rows;
echo $num_rows2;
//if ($result2 = $con->query($stmt2)) {
//if ($result2 = $con->query("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {
if($num_rows2) {
//while ($row2 = $result2->fetch() ) {
while($row2 = $stmt2->fetch()) {
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>";
}
替换:
while($row2 = $stmt2->fetch()) {
与:
while($stmt2->fetch()) {
然后使用以下方法访问值:
$post_id // not $row2['post_id']
bind_result() 函数直接设置这些变量,如 here 所述。