thedevilinu Posted December 25, 2010 Share Posted December 25, 2010 I am trying to run the following code -------------------------- $result = mysql_query("SELECT * FROM indiatutors_profiles WHERE id='$id' ") or die(mysql_error()); while($row=mysql_fetch_array($result)){ $email =$row; } echo $email; ---------------------------- It gives me the following error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in website_path/profiles.php on line 7 The funny thing about the code is that inspite of giving the error, it outputs the email correctly based upon the id which it shouldn't have done if their was some error Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 25, 2010 Share Posted December 25, 2010 The actual code inside your while(){} loop is likely reusing and overwriting the $result variable, so when the while(){} loop is evaluated on the second pass through the loop, $result is no longer a result resource from the first query. Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/#findComment-1151242 Share on other sites More sharing options...
Rifts Posted December 25, 2010 Share Posted December 25, 2010 $email =$row; should be $email =$row['email']; Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/#findComment-1151244 Share on other sites More sharing options...
dragon_sa Posted December 25, 2010 Share Posted December 25, 2010 It should be echoed inside the while loop aswell if there is to be more than 1 result, if there is only supposed to be 1 result then there is no need for the while loop at all Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/#findComment-1151246 Share on other sites More sharing options...
thedevilinu Posted December 25, 2010 Author Share Posted December 25, 2010 @PFMaBiSmAd - I really expect the loop to run just once as their is only a single id associated with any email. You are however right in saying that loop is running more than once. I can't fathom why it is running twice. so changing the code to ------------------------------ $result = mysql_query("SELECT * FROM indiatutors_profiles WHERE id='$id' ") or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo $email =$row; } ------------------ gives me error ---------------- [email protected] Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in website_path/profiles.php on line 7 ---------- which confirms that loop runs twice. Bug was using another $result statement in the code due to which it was running again. Thanks for sorting this out for me. Thanks again @rifts - it works either way mate..my way is just time saving @dragon - echo in or out of the loop .. either way it shouldn't give an error Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/#findComment-1151441 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2010 Share Posted December 26, 2010 $query = "SELECT * FROM indiatutors_profiles WHERE id=$id"; // Presumes that $id is a numeric value. $result = mysql_query($query) or die("<br>Query string: $query<br>Produced error: " . mysql_error()); $row=mysql_fetch_assoc($result); echo $row['email']; // And yes, the array index should be enclosed in quotes. Just because it works without them, doesn't mean it's right. Quote Link to comment https://forums.phpfreaks.com/topic/222612-funny-coding-error/#findComment-1151446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.