elmas156 Posted August 23, 2010 Share Posted August 23, 2010 I've got the following and I'm receiving this error: Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/a3839210/public_html/Testing/main.php on line 37 Can anyone tell me what the problem might be? Thanks very much to anyone who tries to help! <?php include("db.inc.php"); //connects to database $result2 = mysql_query("SELECT messid,message,from,read,date FROM messages WHERE id = '$id'"); // line 36 while ($row2 = mysql_fetch_row($result2)) { // line 37 $message = $row2[1]; // line 38 echo "$message"; // line 39 } // line 40 ?> Quote Link to comment https://forums.phpfreaks.com/topic/211538-while-loop-error/ Share on other sites More sharing options...
Psycho Posted August 23, 2010 Share Posted August 23, 2010 Your query is failing. I suspect it is because you have a field with the name "from" and MySQL is confusing it with the "FROM" for selecting the table. Enclose your field/table names with back ticks. Try this $query = "SELECT `messid`, `message`, `from`, `read`, `date` FROM `messages` WHERE id = '$id'"; $result2 = mysql_query($query) or die("Query:<br />$query<br /><br />Error:<br />".mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/211538-while-loop-error/#findComment-1102821 Share on other sites More sharing options...
samshel Posted August 23, 2010 Share Posted August 23, 2010 Where is $id defined ? - Also echo the query and directly fire it on DB console to see if you see any errors - Use mysql_error() to debug <?php include("db.inc.php"); //connects to database $result2 = mysql_query("SELECT messid,message,from,read,date FROM messages WHERE id = '$id'") or die (mysql_error()); // line 36 while ($row2 = mysql_fetch_row($result2)) { // line 37 $message = $row2[1]; // line 38 echo "$message"; // line 39 } // line 40 ?> Quote Link to comment https://forums.phpfreaks.com/topic/211538-while-loop-error/#findComment-1102822 Share on other sites More sharing options...
Pikachu2000 Posted August 23, 2010 Share Posted August 23, 2010 'from' is a MySQL reserved word. To use it as a field or table name, you must enclose it in backticks (which are not single-quotes): `from` Quote Link to comment https://forums.phpfreaks.com/topic/211538-while-loop-error/#findComment-1102824 Share on other sites More sharing options...
elmas156 Posted August 23, 2010 Author Share Posted August 23, 2010 Thanks everyone! The back ticks worked. Quote Link to comment https://forums.phpfreaks.com/topic/211538-while-loop-error/#findComment-1102830 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.