Becky
Becky

Reputation: 2289

Issues with a mysqli_num_rows statement

I'm having difficulties figuring out why my $numrows variable is getting an error. I get this error..

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given

for this this line...

$numrows = mysqli_num_rows($stmt);

Which is making my else statement of "This topic does not exist display".

What is wrong with my $numrows variable?

$con = mysqli_connect("localhost", "root", "", "");
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 
$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));
}
$numrows = mysqli_num_rows($stmt);
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 = mysqli_fetch_assoc($stmt)){
        //Prepared SELECT stmt to get forum topics
        $stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND topic_id=?");
        if ( !$stmt2 || $con->error ) {
            die('Select topics prepare() failed: ' . htmlspecialchars($con->error));
        }
        if(!$stmt2->bind_param('ii', $cid, $tid)) {
            die('Select topics bind_param() failed: ' . htmlspecialchars($stmt2->error));
        }
        if(!$stmt2->execute()) {
            die('Select topics execute() failed: ' . htmlspecialchars($stmt2->error));
        }
        while($row2 = mysqli_fetch_assoc($stmt2)){
            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>";
}

Upvotes: 2

Views: 91

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

use

$numrows = $stmt->num_rows;

Read this as well

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31749

You are mixing object oriented & procedural style. Should be -

$numrows = $stmt->num_rows;

DOCS

Upvotes: 2

Related Questions