Jump to content


Photo

Select statement in search results page


  • Please log in to reply
2 replies to this topic

#1 bigkev1983

bigkev1983
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 September 2006 - 02:41 PM

Hello there

I'm pretty new to PHP and mianly use Dreamweaver to come up with most of the code for search interfaces to be honest so I'm hoping you guys can help me.

I have this select statement for my search results page:
$query_directory = sprintf("SELECT * FROM directory WHERE Name LIKE '%".$_POST['name']."%' ORDER BY id DESC", $colname_directory);

to get all records from the database whose name contains characters typed in by the user from the search form.

However, it doesn't work and SQL just comes back with an error. The standard statement (below) DOES return results but obviously it doesn't do what I need it to as it forces the user to type in the whole name exactly:

$query_directory = sprintf("SELECT * FROM directory WHERE Name = '".$_POST['name']."' ORDER BY id DESC", $colname_directory);

Can anyone help me make the first statement work?

Kev

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 23 September 2006 - 03:03 PM

sometimes I'll replace the spaces with percents...

$query_directory = sprintf("SELECT * FROM directory WHERE Name = '".str_replace(" ","%",$_POST['name'])."' ORDER BY id DESC", $colname_directory);

#3 bigkev1983

bigkev1983
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 September 2006 - 03:11 PM

Thanks for replying.
When I tried this, I didn't get an error message this time but unfortunately I don't get any results either.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users