SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: How come ?

  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How come ?

    ok, how come this works
    PHP Code:
    $members mysql_query("select email,NewsletterID from Newsletter_Members WHERE email='" 
    $email "' AND NewsletterID=" $NewsletterID$db);

    if (
    mysql_num_rows($members) == ) { 
    But this doesn't?
    PHP Code:
    $members mysql_query("select email from Newsletter_Members WHERE email=" $email$db);

    if (
    mysql_num_rows($members) == ) { 
    Comes up with Warning: Supplied argument is not a valid MySQL result resource in /home/psyfocus/public_html/beta/4/retrive.php on line 17, line 17 is the if statement.

    If email and and newsletter ID are valid, how come email is not valid? There must be a mistake in the code?

    cheers guys!
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just do this.

    $members = mysql_query("select email from Newsletter_Members WHERE email = $email", $db);

    You're making it too complicated for yourself, you can put variables directly into your sql statement
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also try @mysql_num_rows instead of mysql_num_rows.

  4. #4
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Aspen, I did that however still get the same error.

    delemtri, won't that just stop me getting error messages?
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  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)
    Aspen almost had it, since $email contains stff other than numbers it needs to be in single quotes:

    PHP Code:
    $members mysql_query("select email from Newsletter_Members WHERE email = '$email'"$db); 
    Yes prepending any function with @ will supress error messages, however it will not fix your problem and I would suggest not using it while debugging scripts.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it DOES fix your problem. What happens is mysql_num_rows returns 0 when there are no rows, but without the @ it ALSO returns an error. At least, this is my understanding of it. The @ would fix that.

  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)
    Uh sorry delemtri, but that is not correct,

    Warning: Supplied argument is not a valid MySQL result resource in /home/psyfocus/public_html/beta/4/retrive.php on line 17
    This error means the query failed not that mysql_num_rows() is returning 0. mysql_num_rows() will NOT return an error message if it is run on a valid query, depsite how many reocrds are returned.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <sigh> Guess I'm wrong then. I remember reading that somewhere, not to mention I was having the same problem a while back with an entirely valid query... Oh well.

  9. #9
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks freddy, its working now Something tells me you know your stuff!

    with the @ bussiness, my experence is the same as freedy's. But maybe you are using slightly different version of PHP, different OS or your host has set it up strangely?
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    delemtri,

    Again if you are getting an error like

    Warning: Supplied argument is not a valid MySQL result resource in /home/psyfocus/public_html/beta/4/retrive.php
    It means your query has failed, the reason it appears on the line mysql_num_rows() is because that is first time after trying to run the query that you refer to the query result identifier in this case when calling mysql_num_rows($members) so it looks for the result identifier $members and it already knows the query failed so it spits the error message out.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I remember reading it in this thread... <ponder> Oh well.

  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)
    Don't believe everything you read, as that is just not true, just to satisfy my own curiosity I ran another test:

    My table is named test and it has 4 records with the highest id being 4.

    PHP Code:
    <?
    $db 
    mysql_connect("localhost");
    mysql_select_db("datetest");
    $result mysql_query("SELECT * from test");
    print 
    mysql_num_rows($result); //prints 4
    ?>
    PHP Code:
    <?
    $db 
    mysql_connect("localhost");
    mysql_select_db("datetest");
    $result mysql_query("SELECT * from test WHERE id > 5");
    print 
    mysql_num_rows($result); //prints 0
    ?>

    No error messages on either one just a number either 4 for the first one or 0 for the second one. Have you tried testing it yourself delemtri? Try it I think you will pleasantly surprised at the outcome.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I was just relying on my past experience with mysql_num_rows. It could easily have been an error with my code though, unfortunately since then my hosting provider suffered a hard drive failure so I'm going to have to recode the site.


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
  •