1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can a mySQL resource suddenly stop being a resource?!

Discussion in 'PHP' started by Rory M, Jul 20, 2010.

  1. #1
    Hi everyone,

    Having a bit of a nightmare here, absolutely no idea why this isn't working and my eyes are going hazy from looking at it for so long. Any ideas?

    echo mysql_num_rows($done); //Just put in for debugging - works fine
    		
    		if(mysql_num_rows($list) == 0) //Nothing in shop
    		{
    			return 0;
    		}
    		elseif(mysql_num_rows($done) == 0)  //No Previous attempts - all in
    		{
    			return mysql_fetch_array($list);
    		}
    
    		while($done = mysql_fetch_array($done))
    		{
    			$local[] = $done['pref'];
    		}
    PHP:
    It doesn't really matter what's actually going on in the code, I'm fine with the concept.

    $done is a MySql query which executes without error.
    mysql_num_rows($done) returns 1 both times, which is perfectly correct.

    However when it gets to mysql_fetch_array($done), it decides that $done isn't a valid result resource. How is that possible? I've tried putting the query directly into the fetch array, it doesn't help.

    I'd love an SQL wizard to come help me at this point, I'm on the point of assuming that its a PHP quirk created just to spite me!

    Cheers :D
     
    Rory M, Jul 20, 2010 IP
  2. po_taka

    po_taka Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    while($done_new = mysql_fetch_array($done))
    {
    $local[] = $done_new['pref'];
    }

    did it work ?
     
    po_taka, Jul 20, 2010 IP
    Rory M likes this.
  3. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, that worked without a hitch :D +REP. I had previously tried unsetting then resetting, and I'm still not sure exactly why the original way didn't work. If anyone knows, I'd be interested. Thanks so much for the workaround though :)
     
    Rory M, Jul 20, 2010 IP
  4. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You were reassigning $done..

    $done isnt a query in your original post, its the query result...

    in the loop, you were saying, pull the first record from $done, and assign it to $done, so on the second iteration, $done would be a record, rather then a result set
     
    bencummins, Jul 20, 2010 IP
    Rory M likes this.
  5. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Which of course explains why it returned the first result and only threw the error once. Thanks a lot :D
     
    Rory M, Jul 21, 2010 IP