Newbie Need Help on Multiple Select and Limit

Discussion in 'MySQL' started by BlocKid, Oct 11, 2010.

  1. #1
    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?
     
    BlocKid, Oct 11, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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:
     
    jestep, Oct 11, 2010 IP
  3. BlocKid

    BlocKid Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey thanks for the help but I'm not getting the "Total Threads" output. What do I do?
     
    BlocKid, Oct 11, 2010 IP
  4. pkennedy5

    pkennedy5 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $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.
     
    pkennedy5, Oct 14, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    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 :)
     
    koko5, Oct 14, 2010 IP