SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with PHP Code...

    Hi,

    I'm having some problems with some code. Basically, it outputs 1 result and then gives this error:

    Code:
    Warning: Supplied argument is not a valid MySQL result resource 
    in c:\phpdev\www\public\pcreview\new_list.php on line 13
    The code is relatively simple:

    PHP Code:
    <?php
    $news 
    mysql_query("SELECT Name, AuthorID, Title, DateAdded, news.ID as nID, News FROM news, authors " .
    "WHERE AuthorID=authors.ID ORDER BY DateAdded DESC");
    if (!
    $news) {
    print 
    "Cannot query the database.<br>";
    print 
    mysql_error();
    } else {
    ?>
    <blockquote><ul>

    <?php

    while ($result mysql_fetch_array($news)) {
      
    $id   $result["nID"];
      
    $authorid $result["AuthorID"];
      
    $news $result["News"];
      
    $title $result["Title"];
      
    $authorname $result["Name"];
      
    $date $result["DateAdded"];
      
    $title stripslashes($title);
      
    $news stripslashes($news);
      if(
    $date != $olddate) {
          echo 
    "<LI><font face=\"Verdana, Tahoma\" size=\"2\">$date</font><br>\n";

     }
       
    $olddate $date;

         echo 
    " - <font face=\"Verdana, Tahoma\" size=\"2\">$title by $authorname<br>$news<br>\n";


    }
    }
    ?>
    </ul></blockquote>
    Any Ideas what is causing the problem?
    Thanks
    -Peter
    Last edited by petesmc; Oct 9, 2001 at 11:37.

  2. #2
    1-800-JMULDER JMulder's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,745
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it's your line 13

    PHP Code:
    $news mysql_query("SELECT Name, AuthorID, Title, DateAdded, news.ID as nID, News FROM news, authors " .
    "WHERE AuthorID=authors.ID ORDER BY DateAdded DESC"); 
    Try changing it to:

    PHP Code:
    $news mysql_query("SELECT Name, AuthorID, Title, DateAdded, news.ID as nID, News FROM news, authors WHERE AuthorID=authors.ID ORDER BY DateAdded DESC"); 
    Try that..?
    Jeroen Mulder

    w: www.jeroenmulder.com

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That will change nothing.

    Pete did you try copy and pasting the query to your mysql console to see whats being actually returned.


    PHP Code:
    $sql " SELECT Name, AuthorID, Title, DateAdded, news.ID as nID, News FROM news, authors WHERE AuthorID=authors.ID ORDER BY DateAdded DESC";
    print 
    $sql
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your problem starts here..

    if (!$news) {

    whether the query returns any rows or not $news will always have a value (eg ,Resource id #2)...

    and then the error is generated in the while loop on a call to an empty resultset (line 13 )

    you can either use @ to suppress the errors , or use mysql_num_rows()

    PHP Code:
    if(!@mysql_num_rows($news)){
    print 
    "Cannot query the database.<br>";
    print 
    mysql_error();
    } else { .... 
    etc 

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thus pasting the query into the mysql console will tell you why your qeury returned no rows.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    1-800-JMULDER JMulder's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,745
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    That will change nothing...
    Believe it or not, but errors are always the things you least expect

    Serious now, I've had so many wierd errors in my 'PHP Life'..you'll never know. But maybe you're right.
    Jeroen Mulder

    w: www.jeroenmulder.com

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My point was that

    "string string"

    and

    "string". ." string"

    Will produce the same thing and using one over the other is not going to change anything.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    JMulder: I did it like that because vBulletin doesn't wordwrap properly.

    Freddy: Yes, I've tried it and there are two rows as expected.

    Firepages: Thanks for the suggestion, although this doesn't apply as the there are results, as the first one is printed. Great program btw...(phpdev)
    Last edited by petesmc; Oct 9, 2001 at 13:18.

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem lies in this line.

    PHP Code:
    $news $result["News"];
    $news stripslashes($news); 
    You are assigning a vlaue to $news which is going to overwirte the query result stored in $news and thus break the loop after the first iteration. Either change the query result var or change the line above to use something other than $news.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't work...

    Any other ideas?

    -Peter

  11. #11
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked actually...i thought you meant something else to start off with.

    Thanks allot!

    -Peter

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pete I am 99% sure that its the problem, could you please post your new code with those changes, as I think you may have missed something.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /ignore my last.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's funny how one can miss the slightest thing and ruin their day...

    Thanks again

    -Peter


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
  •