【问题标题】:Failing prepared statement. Not getting string准备好的声明失败。没有得到字符串
【发布时间】:2015-10-13 19:08:20
【问题描述】:

这行代码我收到以下错误...

Warning: mysqli::query() expects parameter 1 to be string, object given

        if ($result2 = $con->query($stmt2)) {

我想不通?我的联系是正确的,就像上面准备好的陈述一样。我尝试在整个页面上移动东西并读到这可能是因为我正在混合 mysqli 和 mqsql,但是我的 sql 是怎么做的?

什么会导致这个错误?

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`=?")) {

        //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);
            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();
}

【问题讨论】:

  • $stmt2 必须是没有?的真实查询字符串

标签: php mysql while-loop prepared-statement


【解决方案1】:

这是因为您尝试使用 Prepared Statement OBJECT 执行查询字符串:在您的代码中 $stmt2 是一个准备好的语句对象 $stmt2 = $con->prepare("SELEC

【讨论】:

  • 我在第一个查询中做了同样的事情并且没有得到任何错误?我该如何解决它?
  • 我该如何解决?我还是没完全明白你的意思。
【解决方案2】:

If 条件不适用于assign,它用于compere 的变量。所以改变你的条件

$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) {

         // DO some error message
         }else{
          // Rest code
        }

$stmt2 =$con-&gt;prepare() 在这里你写your query 而不是准备语句$con-&gt;query('your query')

【讨论】:

  • 你的意思是把 if 语句从那一行去掉?
  • $stmt2 =$con-&gt;prepare() 在这里你写你的查询 insted 的准备语句 $con-&gt;query('your query')
  • 我得到了这个错误..Parse error: syntax error, unexpected 'if' (T_IF)
  • 误用if($stmt2===false)我多加了一个括号。
  • 我仍然收到此行的错误...if ($result2 = $con-&gt;query($stmt2)) ....Warning: mysqli::query() expects parameter 1 to be string, object given i
猜你喜欢
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多