SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why won't this simple select and while loop work!? :)

    Hey,
    I have this very simple peice of PHP code. It prints one record but it gives an error:

    Warning: Supplied argument is not a valid MySQL result resource in /web/sites/139/bmxforce/www.bmxforce.f2s.com/news.php on line 28

    You can see it for your self at http://www.bmxforce.f2s.com/news.php

    Here is the code I'm using. Line 28 is the one with the while loop:

    $body = mysql_query( "select * from news order by id desc");

    while ($body_array = mysql_fetch_array($body))
    {
    $body = $body_array["body"];
    $date = $body_array["date"];

    echo "$date $body"

    }


    I feel so stupid! I did the same thing a million times but it won't work, it might be the f2s.com servers...

    Thanks,
    Justin Sampson

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its because you assign the value of $body to a field from your database so then the next time the loop runs $body no longer holds the result identifier for the query. And that is why it only shows one record then dies. try

    PHP Code:
    $result mysql_query"select * from news order by id desc"); 

    while (
    $body_array mysql_fetch_array($result)) 

    $body $body_array["body"]; 
    $date $body_array["date"]; 

    echo 
    "$date $body


    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, the simplest things....

    Thanks Freddy!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way Justin if you use

    extract($body_array); just inside the while loop you can skip the assignment of the fields to variables like:
    PHP Code:
    $result mysql_query"select * from news order by id desc"); 

    while (
    $body_array mysql_fetch_array($result)) 

    extract($body_array);
    echo 
    "$date $body

    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •