tommy168 Posted February 4, 2011 Share Posted February 4, 2011 Hey guys, here's the script: <?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? Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/ Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Your query is failing and returning a boolean FALSE to mysqli_num_rows(). Add some logic to check for query execution success or failure. if( mysqli_query( $dbc, $query) ) { // do whatever } else { echo "<br>Query: $query<br>Failed with error: " mysqli_error($dbc); } Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169975 Share on other sites More sharing options...
drisate Posted February 4, 2011 Share Posted February 4, 2011 You can also do this to get the number of rows $numrows_comm = current(mysqli_fetch_assoc(mysqli_query("SELECT count(name) FROM comments WEHRE blog_id = " .$row['id']."ORDER BY dateposted;"))); Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169977 Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2011 Share Posted February 4, 2011 @drisate, Nesting functions like that prevents you from doing any error checking and error reporting that a real application needs to do. That's the opposite of what should be done. Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169978 Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Why would you even consider nesting all that together? That just makes debugging a nightmare. Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169979 Share on other sites More sharing options...
litebearer Posted February 4, 2011 Share Posted February 4, 2011 Psssst WEHRE should be WHERE Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169980 Share on other sites More sharing options...
drisate Posted February 4, 2011 Share Posted February 4, 2011 When your SQL is good and all you need is the number of rows theres no pointe doing it in 5 lines when you can just go for it like that. But it always depens on what your building. I was just pointing out that he could also do it like that since his gething troble with mysqli_num_rows($commresult) It may look durty, it may smell bad, but at least it works great hehe But you can do it like you want ;-) Link to comment https://forums.phpfreaks.com/topic/226712-mysqli_num_rows-problem/#findComment-1169984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.