So I'm trying to pull 10 threads from my database and the total amount of threads from my database as well. Here's my code ... <?php // Make MySQL Connection require_once('./global.php'); // Retrieve all the data from the "thread" table $query = "SELECT * FROM thread WHERE forumid='2' ORDER BY threadid DESC LIMIT 10"; $query2 = "SELECT * FROM thread ORDER BY threadid DESC LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); // Store the record of the "thread" table into $row while($row = mysql_fetch_array( $result ) or die(mysql_error())){ while($row2 = mysql_fetch_array( $result2 ) or die(mysql_error())){ // Print out the contents of the entry echo " Thread: ".$row['title']; echo " <br /><br />"; echo " Total Threads: ".$row2['threadid']; } ?> Code (markup): My database has about 63 threads. I'm trying to pull 10 from a specific forum which is forumid 2. But I'm also trying to display the total number of threads at the bottom of the page which isn't working. What do I do?
Try this instead: // Store the record of the "thread" table into $row if(!$row2 = mysql_fetch_array( $result2 )) { die(mysql_error()); } while($row = mysql_fetch_array( $result ) or die(mysql_error())){ // Print out the contents of the entry echo " Thread: ".$row['title']; echo " <br /><br />"; } echo " Total Threads: ".$row2['threadid']; PHP:
$query = "SELECT * FROM thread WHERE forumid='2' ORDER BY threadid DESC LIMIT 10"; $query2 = "SELECT * FROM thread ORDER BY threadid DESC LIMIT 1"; These queries look wrong. query2 will always return the same data, which is one line from the db. I think you want something like $query2 = "select count(*) as total from thread" Now the variable total will contain the number of rows in the table, although this isn't the fastest way to do it either.
You've to specify-total number of threads where forum id=2 or just total at all. If first, you can use SQL_CALC_FOUND_ROWS - check the examples. For second case use count(*) as @pkennedy5 said. Regards