View Full Version : Checking if a query string exists?

06-20-2011, 03:15 AM
I have a webpage setup with comments on it. I want the user to be able to click the name of any commentator to see the posts made by only them. I figured having each name link to a query string with the value of itself be simplest.

I want to be able to check whether or not the query string exists. If it does, the MySQL query will filter it all by the query string, otherwise, show all of the comments.

In short, what would be the correct syntax to check if a query string has been set or not?

I've been trying:
6. $query = "SELECT * FROM posts ORDER BY id DESC WHERE username = '$_SERVER['QUERY_STRING']'";
7. results = mysqli_query($cnx,$query);
8. }else{
9. $query = "SELECT * FROM posts ORDER BY id DESC";
10. $results = mysqli_query($cnx,$query);
11. }

But that of course does not work. I get the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/jmcneil/public_html/scripts/comments.php on line 6

06-20-2011, 04:40 AM
Never mind... I figured it out. Apparently I gotta' put the WHERE statement before the ORDER BY one...