mysqli_num_rows() problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

mysqli_num_rows() problem

Post by Vinnar »

Hey guys,

here's the script:

Code: Select all





<?php



require("header.php");



$sql = "SELECT entries.*, categories.cat FROM entries, categories

    WHERE entries.cat_id = categories.id

    ORDER by dateposted DESC

    LIMIT 1;";

$result = mysqli_query($db, $sql);

$row = mysqli_fetch_assoc($result);



echo "<p><h2><a href='viewentry.php?id=".$row['id']."'>'".$row['subject']."</a></h2><br /></p>";

echo "<p>"."<i>In <a href='viewcat.php?id=" . $row['cat_id']."'>'" . $row['cat']."</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted']))."</i></p>";

echo "<p>";

echo nl2br($row['body']);

echo "</p>";



echo "<p>";



$commsql = "SELECT name FROM comments WEHRE blog_id = " .$row['id']."ORDER BY dateposted;";

$commresult = mysqli_query($db, $commsql);

$numrows_comm = mysqli_num_rows($commresult);



if($numrows_comm == 0){



    echo "<p>No comments.</p>";



} else {

    echo "(<b>".$numrows_com."</b>)comments: ";

    $i = 1;

    while($commrow = mysqli_fetch_assoc($commresult)){

    

        echo "<a href='viewentry.php?id=".$row['id']."#comment".$i."'>'".$commrow['name']."</a>";

        $i++;

    

    }



}

echo "</p>";



require("footer.php");





?>





The error turns out to be Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Book\index.php on line 22

why is that?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: mysqli_num_rows() problem

Post by danwguy »

I would try changing it to ...

Code: Select all

 



<?php



require("header.php");



$sql = "SELECT entries.*, categories.cat FROM entries, categories

    WHERE entries.cat_id = categories.id

    ORDER by dateposted DESC

    LIMIT 1;";

$result = mysqli_query($db, $sql);

$row = mysqli_fetch_assoc($result);



echo "<p><h2><a href='viewentry.php?id=".$row['id']."'>'".$row['subject']."</a></h2><br /></p>";

echo "<p>"."<i>In <a href='viewcat.php?id=" . $row['cat_id']."'>'" . $row['cat']."</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted']))."</i></p>";

echo "<p>";

echo nl2br($row['body']);

echo "</p>";



echo "<p>";



$commsql = "SELECT name FROM comments WEHRE blog_id = " .$row['id']."ORDER BY dateposted;";

$commresult = mysqli_query($db, $commsql);

$numrows_comm = mysqli_num_rows($commresult);



if($numrows_comm >= 1){

 echo "(<b>".$numrows_com."</b>)comments: ";

    $i = 1;

    while($commrow = mysqli_fetch_assoc($commresult)){

    

        echo "<a href='viewentry.php?id=".$row['id']."#comment".$i."'>'".$commrow['name']."</a>";

        $i++;

    

}else{

    echo "<p>No comments.</p>";



} 

}

echo "</p>";



require("footer.php");





?>




 
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: mysqli_num_rows() problem

Post by mikosiko »

@Vinnar

most likely because this query

Code: Select all

$commsql = "SELECT name FROM comments WEHRE blog_id = " .$row['id']."ORDER BY dateposted;";
is incorrect "WEHRE" shoulld be WHERE
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

Re: mysqli_num_rows() problem

Post by Vinnar »

nice u pointed that out but still same warning pops up
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysqli_num_rows() problem

Post by social_experiment »

Code: Select all

$commsql = "SELECT name FROM comments WEHRE blog_id = " .$row['id']."ORDER BY dateposted;";
$commresult = mysqli_query($db, $commsql) or die(mysqli_error($db));
What error message do you get change your code to the example above?
It looks like there are some ' marks missing from $row['id'].

Code: Select all

$commsql = "SELECT name FROM comments WEHRE blog_id = '" .$row['id']. "' ORDER BY dateposted;";
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply