...

View Full Version : Php Error advice



PRodgers4284
03-09-2008, 04:26 PM
Im using a script to carryout a search on a mysql database and im getting the following error:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use"

Can anyone provide some info on this, is this something to do with the sql query i am using? Any advice would be must appreciated.

abduraooft
03-09-2008, 04:36 PM
Post your query and the full error.

PRodgers4284
03-09-2008, 04:40 PM
Post your query and the full error.

The full query is:


<?php
include("database.php");

$sql = "SELECT * FROM job";
$jobcat = mysql_real_escape_string(trim($_POST['jobcategory']));
$jobloc = mysql_real_escape_string(trim($_POST['joblocation']));

if ($jobcat != '' && $jobloc != '') {
$sql .= " WHERE jobcategory LIKE '$jobcat%' AND joblocation '%$jobloc%'";
}
else if ($jobcat != '' && $jobloc == '') {
$sql .= " WHERE jobcategory LIKE '%$jobcat%'";
}
else if ($jobloc != '' && $jobcat == '') {
$sql .= " WHERE joblocation LIKE '%$jobloc%'";
}

$query = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($query) > 0)
{
while ($job = mysql_fetch_array($query))
{
$jobtitle=$job["jobtitle"];
$jobcategory=$job["jobcategory"];
?>



The error is:

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 ''%Co.Antrim%'' at line 1

abduraooft
03-09-2008, 04:49 PM
$sql .= " WHERE jobcategory LIKE '$jobcat&#37;' AND joblocation '%$jobloc%'";
You missed the LIKE, the above should be

$sql .= " WHERE jobcategory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";

oesxyl
03-09-2008, 04:52 PM
probably missing a 'like' in first if:


$sql .= " WHERE jobcategory LIKE '$jobcat&#37;' AND joblocation LIKE '%$jobloc%'";

try to see if work. When you have this message you must look in the query where it point and before it, is about syntax:


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 ''%Co.Antrim%'' at line 1


sorry abduraooft, I don't know about your post, edit process, :)


best regards

PRodgers4284
03-09-2008, 04:56 PM
You missed the LIKE, the above should be

$sql .= " WHERE jobcategory LIKE '$jobcat%' AND joblocation LIKE '%$jobloc%'";

Hey thanks for that, that worked fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum