Need help with mysql_fetch_array function

Discussion in 'PHP' started by Imozeb, Jun 18, 2010.

  1. #1
    I need help with this function:


    PHP code:
    
    if ($result && mysql_num_rows($result))
    {
      $numrows = mysql_num_rows($result);
        while($row = mysql_fetch_array($result)) //error here
          {
    
    Code (markup):
    It returns: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
    When I echo out $result it says resource id #9


    Why is it not working?

    Thank you.
     
    Imozeb, Jun 18, 2010 IP
  2. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Could you post up where $result is set?

    Also, using mysql_fetch_assoc() gets a similar result to mysql_fetch_array() but is more efficient to use :)
     
    Zerix, Jun 18, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is everything up to and a little after the error line.

    PHP code:
    //get data
    						require_once('my_db'); 
    						mysql_select_db("my_db");
    				  		$queryString = $_SERVER['QUERY_STRING'];
    						$sql = 'SELECT * FROM `name` WHERE `status` = \'verify\'';
    						
    						//query database for user
    						$result = mysql_query($sql, $my_db) or die(mysql_error().'<br/><br/>'.$query);
    						//loop to find activation key
    						if ($result && mysql_num_rows($result))
    						{
    							$numrows = mysql_num_rows($result);
    							while($row = mysql_fetch_array($result))
    							{
    								if($queryString == $row["akey"])
    								{
    									$id = $row["id"];
    Code (markup):
     
    Last edited: Jun 18, 2010
    Imozeb, Jun 18, 2010 IP
  4. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #4
    require_once('my_db); << You forgot to close the quotes there.
     
    Cozmic, Jun 18, 2010 IP
  5. ZeeshanButt

    ZeeshanButt Well-Known Member

    Messages:
    307
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #5
    require_once('my_db);
    Correct it with closing quote.

    Your code is fine.
     
    ZeeshanButt, Jun 18, 2010 IP
  6. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, as long as status exists in the names table :)
     
    Zerix, Jun 18, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The quote error was when I was copying my code to this forum. The real code has the quotes correctly placed and it still gives the error in the same place. Any ideas on what it could be?


    PS: If the quote really was the error I would have had a different error probably mismatched quotes.
     
    Imozeb, Jun 18, 2010 IP
  8. ZeeshanButt

    ZeeshanButt Well-Known Member

    Messages:
    307
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #8
    I tried your code and run it on my localhost and it worked perfect.
     
    ZeeshanButt, Jun 18, 2010 IP
  9. ZeeshanButt

    ZeeshanButt Well-Known Member

    Messages:
    307
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #9
    are you running it locally?
     
    ZeeshanButt, Jun 18, 2010 IP
  10. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This should work fine...

    
    // Database connection
    require_once('my_db'); 
    mysql_select_db("my_db");
    
    $queryString = $_SERVER['QUERY_STRING'];
    $sql = "SELECT * FROM `name` WHERE `status` = 'verify'";
    
    //query database for user
    $result = mysql_query( $sql, $my_db)or die( mysql_error() );
    $numrows = mysql_num_rows( $result );
    
    //loop to find activation key
    if( $rows > 0 )
    {
    	while( $row = mysql_fetch_assoc( $result ) )
    	{
    		if( $queryString == $row["akey"] )
    		{
    			$id = $row["id"];			
    
    PHP:
    Also, where is $my_db set? in the my_db connection?
     
    Zerix, Jun 18, 2010 IP
  11. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes $my_db is set in the connection require_once().


    mysql_num_rows() works if it is not in the while loop.
    If I put it just above the while loop it works, but my function doesn't work.


    Any idea why this might be the case or is there any way I could get it outside the while loop while performing the same function?
     
    Imozeb, Jun 19, 2010 IP
  12. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #12
    just change...,

    if (mysql_num_rows($result) ==1)
    {

    }
     
    strgraphics, Jun 19, 2010 IP
  13. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    From what I can tell that wouldn't cause a problem, the fact that it is passing would still pass. Though, saying that, if there was no rows and you were trying to loop through would it throw that error?

    From what I have seen here it is safe to say that it is most likely the actual database or the connection that is to blame for the error.
     
    Zerix, Jun 19, 2010 IP
  14. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I found the error. Thanks.
     
    Imozeb, Jun 19, 2010 IP
  15. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    care to share? :)
     
    Zerix, Jun 20, 2010 IP
  16. clox.c

    clox.c Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    yeah, if you could please give us some clarifications, im puzzled with this :p
     
    clox.c, Jun 20, 2010 IP
  17. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Sure. It was a stupid glitch which I should have caught.

    One the line:

    
    
    if($queryString == $row["akey"])
    								{
    									$id = $row["id"];
    }
    [COLOR="red"]else
    {
    //some code
    }[/COLOR]
    Code (markup):
    I had an else command to print out a message if the 'akey' didn't match the query string. When I remd everything in the else out it worked. My solution was to set the error variable first at the beginning of my function and change the error variable if the query string matched to '' so that I could take away the else and still have my function working. :)
     
    Imozeb, Jun 20, 2010 IP