Results 1 to 2 of 2

Thread: Trouble with while loop and mysql_fetch_array not returning multiple rows from db

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Trouble with while loop and mysql_fetch_array not returning multiple rows from db

    Hi chaps,

    I'm having a bit of difficulty and can't seem to get my head around the issue I'm experiencing! As usual have done lots of web searches but haven't managed to turn up the answer.

    I have a query that is returning two rows from the database (confirmed by running the query in MySQL query browser).

    Here's my code:

    PHP Code:
    $connection mysql_connect($host,$user,$password)
                        or die (
    "Couldn't connect to server!");
        
    mysql_select_db($database,$connection)
                        or die (
    "Couldn't select database!");

        
    /* Execute query to retrieve repairs */
        
    $sql "SELECT t2.flat_number,t1.title_of_repair,t1.created_date_time,t3.firstname,t3.lastname
                FROM properties t2
                LEFT OUTER JOIN repairs t1 ON t1.property_id=t2.property_id
                INNER JOIN users t3 on t3.userid=t1.created_by 
                WHERE t1.assigned_to_user=
    $_SESSION[userid] AND t1.signed_off=0";

        
    $result mysql_query($sql)
                        or die (
    "Couldn't execute query!");

        while(
    $row mysql_fetch_array($result,MYSQL_ASSOC));
        {
            echo 
    "test";
            echo 
    "<br />";
        } 
    In reality I'll want to do something slightly more sensible than echo the word "test" of course but I'm using this as proof that the loop is picking up two rows from the database. Now, when I run the above code, it only outputs the word "test" once.

    To be absolutely sure two records are being returned from the query I changed my code for the following in place of the while loop:

    PHP Code:
        $var1 mysql_fetch_row($result);
        
    $var2 mysql_fetch_row($result);

        
    print_r($var1);
        echo 
    "<br />";
        
    print_r($var2); 
    When I run the above code sure enough I can see two arrays containing data corresponding to the two rows returned by the query.

    Why won't the while loop work. No doubt a silly error on my part but it's driving me crazy!

    Hopefully somebody can help?

    Cheers

    Chris.

  2. #2
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hi all,

    Nobody managed to help with this one - but have since solved it - rogue semi colon after the while loop

    while($row = mysql_fetch_array($result,MYSQL_ASSOC));

    Should be

    while($row = mysql_fetch_array($result,MYSQL_ASSOC))

    Chris.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •