SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: while

  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    while

    Hi,

    sorry if my php isn't perfect but I'm just starting out. I'm trying to retrieve the news from a database, it does that but adds this nasty mysql error along after the news:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpdev\www\thehurricanesft\new_site\retrieve02.php on line 43

    Also I can only retrieve one story instead of ten, heres my code:

    PHP Code:
    <?php 

    // connect to mysql 
    require ('connect.php'); 

    //seelct the database 
    $database mysql_select_db("thehurricauk"$dbcnx); 
    if (!
    $dbcnx) { 
        die( 
    '<p>Unable to connect to the ' 
          
    'database at this time.</p>' ); 
       } 

    // selct news stories limit to ten 
    $news = @mysql_query('SELECT headline, newstext, newsdate FROM news_stories 
    ORDER BY id DESC LIMIT 10'
    ); 
    if (!
    news) { 
      die (
    '<p>Error retrieving news from database<br />'
        
    'Error: ' mysql_error() . '</p>'); 


    $result mysql_query($news); 

    while (
    $news mysql_fetch_array($news)) { 
      
    $headline htmlspecialchars($news['headline']); 
      
    $newstext htmlspecialchars($news['newstext']); 
      echo (
    "<div id='date'>" 
      
    .strftime('%d/%m/%y',strtotime($row["newsdate"]))."</div>"
       
    "<div id='image'><h1>$headline</h1>"
       
    "<p>$newstext</p></div>"); 
        } 

    // close database connection 
    mysql_close($dbcnx); 

    ?>

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, you are doing the query twice... remove this line
    PHP Code:
    $result mysql_query($news); 
    although that doesn't affect the result.
    Then change this line
    PHP Code:
    while ($news mysql_fetch_array($news)) { 
    to this
    PHP Code:
    while ($row mysql_fetch_array($news)) { 
    because you were constantly overwriting the $news variable, then you would need to replace $news variable with $row everywhere within the while statement. (except for the part where you do use it )

    Hope that helps
    - website

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't it also be:
    PHP Code:
    while ($row mysql_fetch_array($result)) { 
    as well?

    Hope this helps!

    -Sam
    Sam Hastings

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, not neccisarily since he does the query twice
    - website

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will that actually work, doing the query twice like that? Never seen it done before.

    -Sam
    Sam Hastings

  6. #6
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no it will not, but he didn't use the $result variable after that so it doesn't affect the code
    - website

  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    !?!?!? hmm think that was a mistake still get my head round it, its working know but as programming goes I've run into many more probs but getting there. ;-)


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
  •