douyue8191 2015-07-23 14:40
浏览 43
已采纳

第二份准备好的陈述的背靠背准备陈述的问题

I am attempting to get the following prepared statements to work. I am trying to build a forum and the part I am currently doing is getting the posts from the topic.

The first query works and so does the num rows part. I am able to see the "Add Replay" button. However Once I get to my second query it starts getting a little rough and I can't figure out exactly what is wrong. I have the following error..

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

for this line

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

I have triedto take the if off of my second query's prepared line to help, but it did nothing.

Is that what is breaking this and if so how can I fix. If that isn't what is wrong what is?

$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
    throw new Exception("Connect failed: %s
", 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();
}

UPDATE: CURRENT CODE-

    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>";
        }
  • 写回答

1条回答 默认 最新

  • doujizhong8352 2015-07-23 15:42
    关注

    Replace:

    while($row2 = $stmt2->fetch()) {
    

    with:

    while($stmt2->fetch()) {
    

    Then access the values using:

    $post_id // not $row2['post_id']
    

    The bind_result() function sets those variables directly, as is explained here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?