Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need Help with PHP Syntax Error

New Here ,
Jul 07, 2009 Jul 07, 2009

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');

TOPICS
Server side applications
821
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 08, 2009 Jul 08, 2009

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 do

...
Translate
New Here ,
Jul 07, 2009 Jul 07, 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');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 07, 2009 Jul 07, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 07, 2009 Jul 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 07, 2009 Jul 07, 2009

Okay, let me tell u what the meaning from this code

//select all data from table mainitems

$query_rsMainItems = 'SELECT * FROM mainitems';

//set WHERE as false since WHERE clause isn't use

$where = false;

//check if there's an input from category

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

//add in previous query by filter it using WHERE clause

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

//set WHERE as true, means we have use the WHERE clause in query

$where = true;

}

//check if there's an input from price

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

//check if there's any WHERE set as true from previous query

if ($where) {

//if has, add AND

$query_rsMainItems .= ' AND ';

} else {

//if dont have, use the WHERE

$query_rsMainItems .= ' WHERE ';

//since we have use WHERE clause here, then set is as true

$where = true;

}

//add the variable for AND/WHERE clause

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

}

My suggesstion is, try to use the code one by one to check what's the problem is, I mean test the code using 'category' input first, if okay then go on with second input(price) and so on. I also been in this situation before I get my code working

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 08, 2009 Jul 08, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 09, 2009 Jul 09, 2009
LATEST

Thanks Qiqi and David, you were both right. I really have no idea what was going on so for some reason, maybe on a hunch, I decided to just delete the code and retype it and now it works. Thanks for your great help!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines