I have a query that was working fine, but I recently switched hosts. I couldn't tell you what version of MySQL was on the old host, but I am now on version 5.0.67. The query asks for multiple fields using AND and LIKE. My error message is telling me there may be something wrong with my syntax. Anyone care to help me out? Here's what I got...

<?

  // Get the search variable from URL

  $num = @$_GET['num'] ;
  $loop = @$_GET['loop'] ;
  $type = @$_GET['type'] ;
  $elec = @$_GET['elec'] ;
  $handi = @$_GET['handi'] ;
  $trimmednum = trim($num); //trim whitespace from the stored variable
     
  $var = $num . $loop . $type . $elec . $handi ;
  
// trying to get rid of %
if ($loop=="%") 
  {  $l="Any Loop";  }
if ($type=="%") 
  {  $t="Any Type";  }
if ($elec=="%") 
  {  $e="Both Electric and Non-electric";  }
if ($handi=="%") 
  {  $h="Both Accessible and Not Accessible";  }
  
if (!isset($l))
  {$l=$loop;}
if (!isset($t))
  {$t=$type;}
if (!isset($e))
  {$e=$elec;}
if (!isset($h))
  {$h=$handi;}

// rows to return
$limit=20; 

// check for an empty string and display a message.
if ($var == "")
  {
  echo "<p>Please enter a search...</p>";
  echo "</td></tr></table>";
  include('../footer.php');
  echo "</body></html>";
  exit;
    
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

mysql_connect(...); //(host, username, password)

mysql_select_db(...) or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "SELECT * FROM table1 WHERE (Number LIKE '%$num') 
AND (Loop LIKE '$loop') AND (Site_Type LIKE '$type') 
AND (Electricity LIKE '$elec') AND (Handicap like '$handi') 
ORDER BY Loop, Number";

 $numresults=mysql_query($query) or die(mysql_error());
 $numrows=mysql_num_rows($numresults);

Thanks

Recommended Answers

All 4 Replies

What is the actual error you are getting?

I'm getting:

"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 'Loop LIKE '%') AND (Site_Type LIKE '%') AND (Electricity LIKE '%') AND (Handica' at line 2"

SOLVED - I got some help from another forum. The word "post" is a reserved term, so what I had to do was just change the database field name and then change everywhere it is referred to. Everything works again.

SOLVED - I got some help from another forum. The word "post" is a reserved term, so what I had to do was just change the database field name and then change everywhere it is referred to. Everything works again.

instead you could have used single quatation at the table's name, it will work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.