View Full Version : only show records for the user logged in

05-16-2008, 03:35 PM
I only want to display the records that are assignedto user currently logged in?!
the error i get is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /display1.php on line 18

$result = mysql_query("SELECT * FROM assessment WHERE ".$_SESSION["login"]." = assignedto ORDER BY Dateadded DESC ");

05-16-2008, 03:45 PM
Unless the value of $_SESSION['login'] is an integer, that's an invalid query. You must wrap all string values in quotes (it doesn't hurt to wrap integers in quotes either - MySQL knows what to do with them). The comparison is also backwards. I don't know that I've ever seen it written that way, I suppose it would work, I haven't tested it.

Your code should be written as

$query= "SELECT * FROM assessment
WHERE assignedto = '{$_SESSION['login']}'
ORDER BY Dateadded DESC";
$result= mysql_query($query)
OR die( "QUERY ERROR:<br />{$query}<br />" .mysql_error() );

Notice four things:

Separation of the SQL statement from the query call. You can then take that string and display in the browser, send to a log, etc.
That you can use whitespace to better organize your SQL statements. I'm fond of using HEREDOC syntax when the mood strikes.
The use of {braces} to wrap the array variable. I typically use these when including variables in strings that need to be evaluated, but it's essential to properly evaluate $array['indexes'].
Use of the old standby die() to kill the script and send an error message. Note this should only be done while developing. A "live" server should only ever handle errors gracefully, present the user will a full document while silently logging the error for you.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum