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
https://forums.phpfreaks.com/topic/113268-solved-php-sql-sytanx-error-near-like/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.