2

I have the following two prepared statements. The db connection and queries are correct, I have tested them within phpmyadmin. I also tested inside of my while fetch loop to see if I am pulling the data I am supposed to be and I am.

The problem resides in my while and foreach loops or possibly my num rows statement. I am not sure what I am doing incorrectly in there.

I am getting this error:

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

For this while loop:

        while ($row2 = $result->fetch_assoc() ) {

I am also getting my else statement..

    echo "<p>This topic does not exist.</p>";

Even though the info is echoing out correctly, again I just think my loops are wrong?

Does anyone see what I am doing wrong in my loops?

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

        if (!$stmt) {
            throw new Exception($con->error);
        }
    }



while ($row = $stmt->fetch()) { 

     $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>";
        }
        }


    foreach($stmt as $row) {

        //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->bind_param("ii", $cid, $tid);
            $stmt2->execute();
            $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 ($result = $con->query($stmt)) {
            while ($row2 = $result->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();
}
Paul
  • 3,180
  • 2
  • 20
  • 50

1 Answers1

2

if you search on $stmt with ctrl-F in your browser (and ignoring $stmt2), you will notice that $stmt is a prepared statement all the way down to the error line. $stmt comes to life as a return type from prepare, is bound, and executed.

later on you:

if ($result = $con->query($stmt)) {

so $con->query() is expecting a string, not an object, no?

From the manual.

Not that there aren't other things to consider under a microscope, but I hope this narrowly answers the error message for you.

Edit:

Apparently, you cannot use bind_result with select *. Read the gents Accepted Answer to this question. He does 2 examples, 1 with 1 without select *. Also note store_result()

Here is the link to his answer that was upvoted quite a bit.

Community
  • 1
  • 1
Drew
  • 24,120
  • 9
  • 38
  • 72
  • Ok, I changed that line to reflect $stmt2. The part I don't get is the numrows part. I know it is fetching the correct id's because I checked for it. So I echoed `$numrows` and it said 1, which is should be. BUT for some reason now I cannot get it to echo 1 anymore, it says 0. BUT even when it was echoing 1, I was not getting any thing from the first table that says add reply or else login. I will edit my question to show the changes. – Paul Jul 23 '15 at 03:20
  • I still get that object error after making `$stmt` `$stmt2`? – Paul Jul 23 '15 at 03:33
  • I know look at Edit @ bottom – Drew Jul 23 '15 at 03:36
  • I tested it on my second query `$stmt2->bind_param("ii", $cid, $tid); $stmt2->execute(); $stmt2->store_result(); $result2 = $stmt2->get_result();` And I got this error: `Fatal error: Call to undefined method mysqli_stmt::get_result() i` – Paul Jul 23 '15 at 03:41
  • in your comment a few lines above, that is a block that does **not** use `select *` .... in which case that guy was using `bind_result` and you are using `get_result` .... not to mention you are probably not using the `native driver` – Drew Jul 23 '15 at 03:52
  • makes for fun and enjoyable debugging, now doesn't it :> – Drew Jul 23 '15 at 03:54
  • I added in the 'SELECT *' so I had it right. How can I change my library to the native one? Do you know off hand? – Paul Jul 23 '15 at 03:55
  • Also, I am using SELECT * and bind_results in my first query and it works fine? – Paul Jul 23 '15 at 03:56
  • gonna skirt that question for a second. how about if you dont use select * and do bind result – Drew Jul 23 '15 at 03:56
  • i am fishing cuz i can't test it – Drew Jul 23 '15 at 03:57
  • Just tried that and it still gives that object error. – Paul Jul 23 '15 at 03:58
  • nd answer here about php.ini perhaps, heavily upvoted http://stackoverflow.com/a/8343970/1816093 – Drew Jul 23 '15 at 03:59
  • Everything I see when searching say that error is mixing mysqli and mysql? Also possibly mixing prepared and OOP mysqli. What is wrong with `if ($result2 = $con->query($stmt2)) {` – Paul Jul 23 '15 at 04:02
  • Cuz it hits the procedural version perhaps – Drew Jul 23 '15 at 04:06