While Loop not returning results..?

Discussion in 'Databases' started by colinjw, Jun 12, 2013.

  1. #1
    Hi there, my first echo in the code below returns the page title but the second echo doesn't - I can't work out why. Can anyone help please?

    <?php
     
        // Retrieve the page's name:
        $q = "SELECT title FROM pages";
        $r = mysqli_query ($dbc, $q);
       
            echo 'Test the $row ' . $row[0] . '<br />';
       
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        echo 'Title ' . $row[0];
     
    } // End of while loop.
    PHP:
    Thank you,
    Colin
     
    colinjw, Jun 12, 2013 IP
  2. TanzimH

    TanzimH Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Mate, you are misusing the fetch array wrongly, the loop will go through each row one by one rather than you trying to access the index of 0 of the array. Where it says $row inside the square brackets you will need to have the field name of the database for example $row['page_title'];
     
    TanzimH, Jun 13, 2013 IP
  3. BradsFX

    BradsFX Active Member

    Messages:
    92
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    55
    Digital Goods:
    2
    #3
    <?php
     
        // Retrieve the page's name:
        $q = "SELECT title FROM pages";
        $r = mysqli_query ($dbc, $q);
     
            echo 'Test the $row ' . $row['page_title'] . '<br />';
     
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        echo 'Title ' . $row['page_title'];
     
    } // End of while loop.
    PHP:
     
    BradsFX, Jun 14, 2013 IP