beansandsausages Posted February 26, 2008 Share Posted February 26, 2008 Hey all. I have a script that has been working fine, now all of a sudden i get this error : Warning: mysql_fetch_assoc(): 5 is not a valid MySQL result resource in c:\phpdev\www\public\mem.php on line 275 Thhis is the block of code the error line is referring to : $sql = "SELECT * FROM `news` ORDER BY id DESC LIMIT 0,7"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "<p>No news posted. </div></p>"; include_once("layout_all/bottom.php"); exit; } while ($row = mysql_fetch_assoc($result)) { $row[message] = str_replace($word, $image, $row[message]); $row[message] = str_replace($color, $text, $row[message]); $row[message] = str_replace($effect, $status, $row[message]); echo "<p> {$row["date"]} : {$row["time"]} : Poster {$row["poster"]} <br /> {$row["message"]} </p> <br /> "; if($info[security] >= 8 ) { echo "<p><form action=\"?action=delete\" method=\"POST\"><input type=\"hidden\" name=\"delete_id\" id=\"delete_id\" value=\"{$row['id']}\"><input type=\"submit\" value=\"Delete Post\"> </form> <form action=\"?action=edit\" method=\"POST\"><input type=\"hidden\" name=\"edit_id\" id=\"edit_id\" value=\"{$row['id']}\"><input type=\"submit\" value=\"Edit Post\"></form></p> <br />"; } mysql_free_result($result); echo " </div> "; } Any ideas people thanx Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 The problem is that $result contain null... because the sql statement was incorrect or the connection was incorrect Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 it kinda works. it returns 1 result Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 Then error_reporting(~E_ALL); and you won't see warnings Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 that wont solve the problem as it dont run all the script. Quote Link to comment Share on other sites More sharing options...
fenway Posted February 26, 2008 Share Posted February 26, 2008 see what mysql_error() has to say after the query() call. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 where would you put the error? Just i have tried several diffrent places and recived no error. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 i use the same query in a diffrent page and it works fine shows the limit number of records why i dont understand why the error Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 what connection are you using?? maybe there is some kind of error while connecting to the db Quote Link to comment Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 see what mysql_error() has to say after the query() call. You'd better do this EVERY TIME you make a call to a mysql function Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 i do if you read the script , Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 what connection are you using?? maybe there is some kind of error while connecting to the db connection is fine. i have no idea why the error cos its working fine in the other scripts. Quote Link to comment Share on other sites More sharing options...
fenway Posted February 26, 2008 Share Posted February 26, 2008 Which one is line 275? Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 Which one is line 275? Line 275 : while ($row = mysql_fetch_assoc($result)) { You require any more lines? Quote Link to comment Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 i do if you read the script , I'm afraid you're right... But could be you free the result inside the reading cycle? I can't clearly see where are all of your brackets... Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 I have tried several diff variations of the code same error, but when i load the script up on a diffrent page {exactly the same apart from the limit is higher} it works Quote Link to comment Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 could be you free the result inside the reading cycle What about this...? Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 i took if off and it works so how that happen? Quote Link to comment Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 It happens that you can't have it inside your cycle Actually you don't need it at all... anyway I don't believe it can work on another site with that code, unless you had LIMIT 1 in the query Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted February 26, 2008 Author Share Posted February 26, 2008 that makes sence because i incresed the limit from 1 to 7 hm.. thanx Quote Link to comment Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 In case you did not understand, that instruction "kills" your result. Actually, as it was inside the cycle, you got the error when you tried to read the second line. That's why 'limit 1' works. It makes sense to have it when you're finished with your result. Anyway you don't need it (php does it for you at the end of the script), unless you need much more time and/or memory after processing your result. Quote Link to comment 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.