Forum Moderators: coopster

Message Too Old, No Replies

php search - more than one word makes error

         

j milo taylor

2:07 pm on May 29, 2008 (gmt 0)

10+ Year Member



I am building a site and want to put in a search engine; It's working ok, but when i put in more than one word, I get an error.

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 'notes_work LIKE '%box%'' at line 1

My code is:

<?php

include('Connections/immapp.php');
// set your infomation.

// connect to the mysql database server.
mysql_connect ($hostname_immapp, $username_immap, $password_immapp);
//select the database
mysql_select_db($database_immapp) or die('Cannot select database');

if(isset($_GET['search']))
{
$search = $_GET['search'];
}

$keywords = explode(" ", $search);

$query = "SELECT id_work, name_work, worktype_work, notes_work, artistID_work FROM work " .
"WHERE notes_work LIKE '%".$keywords['0']."%' OR worktype_work LIKE '%".$keywords['0']."%' OR name_work LIKE '%".$keywords['0']."%' ";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." notes_work LIKE '%".$keywords[$i]."%'";

}

$result = mysql_query($query) or die(mysql_error());
$totalRows_result = mysql_num_rows($result);
?>

Can anyone help?

Regards
Milo

jatar_k

2:35 pm on May 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the best bet is to echo the query and then first, look at it, then work on it directly in mysql using the command line, phpmyadmin or some other mysql mgmt tool

WesleyC

2:38 pm on May 29, 2008 (gmt 0)

10+ Year Member



Instead of:

for ($i=1; $i<count($keywords); $i++) {
$query = $query." notes_work LIKE '%".$keywords[$i]."%'";

Try:

for ($i=1; $i<count($keywords); $i++) {
$query = $query." OR notes_work LIKE '%".$keywords[$i]."%'";