Please help me with this php code - getting errors

Discussion in 'PHP' started by bluemouse2, Aug 4, 2009.

  1. #1
    I keep getting this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on first line of this code:

    The code is:

      $sql = "select * from sponsored_jokes where jokeid = $jokeid";
    
       $result = mysql_query($sql ,$db);
    
       if ($myrow = mysql_fetch_array($result)) {
    
         do {
    
            $sponsorid = $myrow["sponsorid"];
    
         } while ($myrow = mysql_fetch_array($result));
    
       }
    Code (markup):
    It worked fine on PHP4. Now it does not on PHP5.

    Please help. Thank you.
     
    bluemouse2, Aug 4, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Add this after the mysql_query() line: (It should show us the error message)

    
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
    PHP:
     
    premiumscripts, Aug 4, 2009 IP
  3. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Thank you. I get these:

    Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
     
    bluemouse2, Aug 4, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I think the syntax is correct :confused: but it looks better like this:

    
    $sql = "SELECT * FROM sponsored_jokes WHERE jokeid = $jokeid";
    
    PHP:
    Most importantly, Is $jokeid null?
     
    ads2help, Aug 4, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Could you also add echo $sql;
     
    premiumscripts, Aug 4, 2009 IP
  6. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #6
    I think the script can't see the $jokeid value...... but it seems to connect to the database!
     
    bluemouse2, Aug 4, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, where are you getting $jokeid from? Where is it initialized?

    Shouldn't it be $_GET['jokeid']? (be sure to use mysql_real_escape_string($_GET['jokeid']) in your SQL)
     
    premiumscripts, Aug 4, 2009 IP
  8. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #8
    even when I write $sql = "select * from sponsored_jokes where jokeid = 2";

    i still get that error instead of displaying joke number 2
     
    bluemouse2, Aug 4, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Try to do that same SQL in phpmyadmin and see what it says there.
     
    premiumscripts, Aug 4, 2009 IP
  10. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #10
    Try:
      $sql = "SELECT * FROM `sponsored_jokes` WHERE `jokeid` = '$jokeid'";
    
       $result = mysql_query($sql, $db) or die(mysql_error());
    
       if ($myrow = mysql_fetch_array($result)) {
    
         do {
    
            $sponsorid = $myrow["sponsorid"];
    
         } while ($myrow = mysql_fetch_array($result));
    
       }
    Code (markup):
     
    Sky AK47, Aug 4, 2009 IP
  11. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #11
    The error disappears but I get no content instead :( Thank you anyway.


     
    bluemouse2, Aug 4, 2009 IP
  12. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #12
    $sponsorid is empty?

    $sql = "SELECT * FROM `sponsored_jokes` WHERE `jokeid` = '$jokeid'";
    $result = mysql_query($sql, $db) or die(mysql_error());
    while($myrow = mysql_fetch_array($result)){
        $sponsorid = $myrow["sponsorid"];
    }
    
    Code (markup):
    Not liking the do thing.
     
    Sky AK47, Aug 4, 2009 IP
  13. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #13
    Still not working.... thank you anyway! It seems the script can't access the db correctly.... however it does access it..but only partially.
     
    bluemouse2, Aug 4, 2009 IP
  14. codedeep

    codedeep Peon

    Messages:
    136
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    add single quotes to var : $sql = "SELECT * FROM sponsored_jokes WHERE jokeid = '".$jokeid."'";
     
    codedeep, Aug 5, 2009 IP
  15. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #15
    is $myrow set anywhere before this line:
    if ($myrow = mysql_fetch_array($result))
    ?
     
    ezprint2008, Aug 5, 2009 IP
  16. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #16
    thank you everyone. the problem was solved with a script upgrade.
     
    bluemouse2, Aug 5, 2009 IP