Releasing MYSQL data

Discussion in 'PHP' started by m_ax, Jul 26, 2007.

  1. #1
    Hi, I'm new to this forum, and would appreciate any help on this matter grealty.

    I have a MYSQL database set up with a table within it called faq. This in turn has the subcatorgaries faq_id which is the Primary Key and faq_title and faq_results which store text segments. I want to read all this data and display it in a list format which is organised by the faq_id, I have almost achieved this with the code that I have written however I have stummbled across a problem which probably due to my incompetence and easy to sort out, however I personally cannot see a way to fix it.

    The problem is that the PHP I have wirtten will read the MYSQL table, but will not display all the results in the Table only the first or the last result depending on how I order it, can anyone suggest a way I can fix this problem I will enclose the code below;

    <?php 
    $mysqli = mysqli_connect("localhost", "root", "password", "mydb");
    if (mysqli_connect_errno()) {
      printf("Could not Connect", mysqli_connect_error());
      exit();
      } else {
        $sql = "SELECT faq_id, faq_title,faq_reply FROM faq ORDER BY faq_id ASC";
        $res = mysqli_query($mysqli, $sql) or die (mysqli_error($mysqli));
        
          if ($res) {
              $faq_info = mysqli_fetch_array($res);
              $faq_id    = $faq_info['faq_id'];
    		      $faq_title = $faq_info['faq_title'];
    		      $faq_reply = $faq_info['faq_reply'];
    		    
              $display_block .=  "<p>".$faq_id." - ".$faq_title."<br>"
             .$faq_reply."</p><br>" ;
            }
            else {
            printf("Failed", mysqli_error($mysqli));
            }
          mysqli_close($mysqli);
          }  
      
    
    ?>
    Code (markup):
    Any help will be greatly appriciated. Thanks in advance
     
    m_ax, Jul 26, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You basically need to loop through the results to get them all to display.

    
    
    if ($res) {
    
    while($faq_info = mysqli_fetch_array($res)){
              $faq_id    = $faq_info['faq_id'];
    		      $faq_title = $faq_info['faq_title'];
    		      $faq_reply = $faq_info['faq_reply'];
    		    
              $display_block .=  "<p>".$faq_id." - ".$faq_title."<br>"
             .$faq_reply."</p><br>" ;
                }
            }
    
    
    PHP:
     
    jestep, Jul 26, 2007 IP
  3. m_ax

    m_ax Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Oh, so easy. Doh, thought it needed to loop or something but couldn't thnk of a feasible way to make it work. Thank you soo much, your help is much appreciated the extra bit of code you supllied works beautafully :)
     
    m_ax, Jul 26, 2007 IP