mysql_num_rows() problem

Discussion in 'PHP' started by Arson, Feb 23, 2009.

  1. #1
    For some reason this bit of code:
    
    <?
    
    $_GET['offer']=strip_tags($_GET['offer']);
    $_GET['offer']=mysql_real_escape_string($_GET['offer']);
    $type="free";
    $getoffers=mysql_query("select * from offers where `type`='$type' and active=1 order by reward desc",$c);
    
    if(mysql_num_rows($getoffers)==0) //ERROR LINE
    {
    print"<tr><td colspan=\"3\">There are currently no free offers available</td></tr>";
    }
    ?>
    
    Code (markup):

    is producing the error:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /path/to/public_html/domain/offers.php on line ###
    Code (markup):

    The same error is coming up in other parts of my script using the mysql_num_rows() function. I am thinking that it has to do with the PHP version, but cannot be sure.
    If anyone can help me out it would be immensely appreciated!!
     
    Arson, Feb 23, 2009 IP
  2. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    After query add line:
    echo mysql_error();
    PHP:
    and tell us what it shows.

    If nothing then try
    var_dump($getoffers);
    PHP:
    and paste result here.

    We need something to work with, to help you out.
     
    Greg Carnegie, Feb 23, 2009 IP
  3. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Don't take this the wrong way but this code is a mess... try this instead.

    $offer=mysql_real_escape_string(strip_tags($_GET['offer']));
    $type="free";

    $query = "select * from offers where type='$type' and active=1 order by reward desc";
    $sql = mysql_query($query);
    $availableoffers = mysql_num_rows($sql);

    if($availableoffers){
    // do whatever if offers exist.
    }else{
    print"<tr><td colspan=\"3\">There are currently no free offers available</td></tr>";
    }
     
    websecrets, Feb 23, 2009 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    hmm, looking at your code, there seems to be a problem with your query...
     
    creativeGenius, Feb 23, 2009 IP
  5. Lpe04

    Lpe04 Peon

    Messages:
    579
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    type shouldn't be in quotation marks
     
    Lpe04, Feb 23, 2009 IP
  6. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #6
    <?
    $offer = $_GET['offer'];
    $strip_offer=strip_tags($offer);
    $escape_offer=mysql_real_escape_string($strip_offer);
    /* Now you can user your offer value from $escape_offer variable */
    $type="free";
    $getoffers=mysql_query("select * from offers where type='$type' and active=1 order by reward desc",$c);
    if(mysql_num_rows($getoffers)==0) //ERROR LINE
    {
    print"<tr><td colspan=\"3\">There are currently no free offers available</td></tr>";
    }
    ?>
    PHP:
     
    ActiveFrost, Feb 23, 2009 IP
  7. Arson

    Arson Well-Known Member

    Messages:
    622
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    120
    #7
    How dare you!
    Just kidding. I know my code could be cleaned up a bit. Nasty old habits.
    :)



    Also, thanks to all for the input.
    Problem has been resolved.
     
    Arson, Feb 23, 2009 IP
  8. Lpe04

    Lpe04 Peon

    Messages:
    579
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    what was it?
     
    Lpe04, Feb 23, 2009 IP
  9. Arson

    Arson Well-Known Member

    Messages:
    622
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    120
    #9
    I'm not quite sure since I'm not the one that fixed it lol.
    However, I did notice that the type was still in quotation marks, if that is something you're wondering about lol
     
    Arson, Feb 23, 2009 IP