Skip to main content
Known Participant
July 7, 2009
Answered

Need Help with PHP Syntax Error

  • July 7, 2009
  • 1 reply
  • 817 views

I am struggling with this error message "Parse error: syntax error, unexpected T_IF" for line 66 that I keep getting. Here's the code that I'm using that I got with help from users of this website, which is supposed to run a multiple parameter search on a mysql database. The red text is line 66. I am just beginning to learn PHP and I can't figure out what to do with it. Is it possible to have two if statements following each other like that? Also, as I mentioned, I was given this code from help from this forum, but I don't understand what the purpose is of the variable $where. Will this code also work without it? Thanks in advance for any help.

$query_rsMainItems = 'SELECT * FROM mainitems'; $where = false; if (isset($_GET['category']) && !empty($_GET['category'])) {      $query_rsMainItems .= ' WHERE category = '. GetSQLValueString($_GET['category'], 'text');      $where = true; } if (isset($_GET['price']) && !empty($_GET['price'])) {   if ($where) {      $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'price = ' . GetSQLValueString($_GET['price'], 'text'); } if (isset($_GET['time1']) && !empty($_GET['time1'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'time1 = ' . GetSQLValueString($_GET['time1'], 'text'); } if (isset($_GET['time2']) && !empty($_GET['time2'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'time2 = ' . GetSQLValueString($_GET['time2'], 'text'); } if (isset($_GET['city']) && !empty($_GET['city'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'city = ' . GetSQLValueString($_GET['city'], 'text'); } if (isset($_GET['zip']) && !empty($_GET['zip'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'zip = ' . GetSQLValueString($_GET['zip'], 'text'); } if (isset($_GET['neighborhood']) && !empty($_GET['neighborhood'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';   }   $query_rsMainItems .= 'neighborhood = ' . GetSQLValueString($_GET['neighborhood'], 'text');

This topic has been closed for replies.
Correct answer David_Powers

I have copied and pasted the code into a local page. Apart from a missing curly brace after the final line ($query_rsMainItems .= 'neighborhood = ' . GetSQLValueString($_GET['neighborhood'], 'text');), it works perfectly.

The error message says there's an unexpected T_IF on line 66. That means the PHP engine isn't expecting a conditional clause at that point. In all probability, there is an error at an earlier stage in the code, which has resulted in an unmatched brace.

The code that I provided does work. I tested it fully before originally posting it, and have tested it again. The problem is that I'm testing it in isolation from the rest of the code on your page. Without seeing the whole page, it's impossible to determine where the error lies.

1 reply

aspenjianAuthor
Known Participant
July 7, 2009

That code didn't come out right, let me try again. The bold and underlined text is line 66, for which I'm getting the error message.

59     $query_rsMainItems = 'SELECT * FROM mainitems';

60     $where = false;

61     if (isset($_GET['category']) && !empty($_GET['category'])) {

62          $query_rsMainItems .= ' WHERE category = '. GetSQLValueString($_GET['category'], 'text');

63          $where = true;

64     }

65     if (isset($_GET['price']) && !empty($_GET['price'])) {

66       if ($where) {

     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'price = ' . GetSQLValueString($_GET['price'], 'text'); } if (isset($_GET['time1']) && !empty($_GET['time1'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'time1 = ' . GetSQLValueString($_GET['time1'], 'text'); } if (isset($_GET['time2']) && !empty($_GET['time2'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'time2 = ' . GetSQLValueString($_GET['time2'], 'text'); } if (isset($_GET['city']) && !empty($_GET['city'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'city = ' . GetSQLValueString($_GET['city'], 'text'); } if (isset($_GET['zip']) && !empty($_GET['zip'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';     $where = true;   }   $query_rsMainItems .= 'zip = ' . GetSQLValueString($_GET['zip'], 'text'); } if (isset($_GET['neighborhood']) && !empty($_GET['neighborhood'])) {     if ($where) {     $query_rsMainItems .= ' AND ';   } else {     $query_rsMainItems .= ' WHERE ';   }   $query_rsMainItems .= 'neighborhood = ' . GetSQLValueString($_GET['neighborhood'], 'text');

July 8, 2009

This code had been provided in David Power's book and it was okay for me. I cant figure out what's the problem from your code but I just need to ask u one thing, did u put } symbol after

$query_rsMainItems .= 'neighborhood = ' . GetSQLValueString($_GET['neighborhood'], 'text');

in your coding?

aspenjianAuthor
Known Participant
July 8, 2009

Yes, I did, I guess I just didn't copy and paste it. I'm so baffled by this because there doesn't appear to be anything wrong. Do you know what the purpose of line 66 is though with the "if ($what)" statement, I don't get the point of it or what purpose it serves in the code, but I got it form David Powers, as you said.