Jump to content

[SOLVED] PHP SQL sytanx error near LIKE


abch624

Recommended Posts

Hi Guys,

 

I have a problem here that gives me this 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 near '%Zahid%' at line 1

 

Now the code that searches produces this is

<?php 


$tbl_personal="personaldetails"; // Table name for personal details
$tbl_profile="profiledetails"; // Table name for profile details
$tbl_cities="tblcities"; // Table name for city name
$tbl_countries="tblcountries"; // Table name for country name
$tbl_branches="tblbranches"; // Table name for branch name
$tbl_hidden="hidden"; // Table name for hidden values

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Variable from url
$query = $_GET['query'];

// Variables sent from form
$searchfield=$_POST['searchField'];

echo $sql = "SELECT DISTINCT P.firstName, P.lastName, P.bid, P.lid, D.profilePicture FROM $tbl_personal P JOIN $tbl_profile D ON (P.profileDetailsID = D.profileDetailsID) JOIN $tbl_cities C ON (P.lid = C.lid) JOIN $tbl_branches B ON (P.bid = B.bid) WHERE P.firstName LIKE %$query%";
echo "<hr>";
if ($result = mysql_query($sql)) {
$row=mysql_fetch_array($result);
$lastname = $row['lastName'];
$profilepicture = $row['profilePicture'];
}
else {
echo mysql_error();
}

?>

 

I echo the sql statement and I get

SELECT DISTINCT P.firstName, P.lastName, P.bid, P.lid, D.profilePicture FROM personaldetails P JOIN profiledetails D ON (P.profileDetailsID = D.profileDetailsID) JOIN tblcities C ON (P.lid = C.lid) JOIN tblbranches B ON (P.bid = B.bid) WHERE P.firstName LIKE %Zahid%

 

This means the sql statement is perfectly fine...

 

I would like to know the correct syntax I should use inorder to use the "LIKE %text%"

 

Please help.

 

Thanks - Zahid

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.