damdempsel Posted October 25, 2009 Share Posted October 25, 2009 I created a script that allowed users to send and receive messages from each other. It seemed to be working but now I get the error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Any ideas on how to fix this? Here is the code: <?php include 'functions.php'; if (!loggedin()) { header("Location: userarea.php"); } $username = $_COOKIE['username']; $query = "SELECT * FROM message WHERE to = '".$to."' ORDER BY id DESC"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $to = $row['to']; $from = $row['username']; $message = $row['message']; $date = $row['date']; echo "<font color=\"#ffffff\">"; echo nl2br($message); echo "</font>"; echo "<font color=\"#ffffff\"><br>-$from<br><br>Recived on $date<br<br><br><br>_____________________<br></font>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/ Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 You normally get that warning when the SQL query is invalid. Try amending... or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR); ... to the end of all calls to mysql_query(). Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944092 Share on other sites More sharing options...
damdempsel Posted October 25, 2009 Author Share Posted October 25, 2009 I added or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR); at the end of my sql line and now I have this error: Fatal error: SQL: SELECT * FROM message WHERE to = '' ORDER BY id DESC, ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to = '' ORDER BY id DESC' at line 1 Ehh, just noticed that $to is in the wrong place. I changed it to be $username like it should be. I get the same error though. It just has my username in it. Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944099 Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 If you get exactly the same error message then it is saying that $username doesn't have a value. Since that appears to be coming from a cookie have you checked that cookie exists? Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944106 Share on other sites More sharing options...
mrMarcus Posted October 25, 2009 Share Posted October 25, 2009 where is $to defined? Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944108 Share on other sites More sharing options...
damdempsel Posted October 25, 2009 Author Share Posted October 25, 2009 There is a cookie that is saved as my username. Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944110 Share on other sites More sharing options...
damdempsel Posted October 25, 2009 Author Share Posted October 25, 2009 where is $to defined? It is defined after the sql command is sent through. I switched it with $username. The error I have now is: Fatal error: SQL: SELECT * FROM message WHERE to=Damdempsel ORDER BY id DESC, ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to=Damdempsel ORDER BY id DESC' Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944115 Share on other sites More sharing options...
mrMarcus Posted October 25, 2009 Share Posted October 25, 2009 you wrapping $to in the query with single quotes? Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944119 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2009 Share Posted October 25, 2009 to is a reserved mysql keyword and that query never worked. What query were you using there that did work? Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944123 Share on other sites More sharing options...
damdempsel Posted October 25, 2009 Author Share Posted October 25, 2009 Not quite sure how I did it, but it seems to work fine now. Thanks to all who helped. Quote Link to comment https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/#findComment-944129 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.