Jump to content

[SOLVED] mysql_fetch_assoc()


damdempsel

Recommended Posts

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>";

                                     

                                      }

                                 

                                           

                                 

                               

 

 

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/178946-solved-mysql_fetch_assoc/
Share on other sites

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.

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'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.