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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/ 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476919 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476923 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476925 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. Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476927 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. Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476939 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. Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476942 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-476994 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477015 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477020 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 , Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477042 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. Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477045 Share on other sites More sharing options...
fenway Posted February 26, 2008 Share Posted February 26, 2008 Which one is line 275? Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477070 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? Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477077 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... Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477082 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477084 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...? Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477090 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? Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477100 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477102 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 Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477106 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. Link to comment https://forums.phpfreaks.com/topic/93089-error-please-help-me/#findComment-477117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.