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.
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
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):
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.
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?
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?
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.
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.