PHP MYSQL search code help!

G

Guest

Guest
Hi,

I would be very greatful if you could give me a little help with my php mysql search code.

The problem I have is that when i search a record in my table it will only search the first word. i.e. if the record is 'bob smith' and I search for 'smith' it won't find the record, when it would if I searched for 'bob'.

I'm sure it's something very simple - thanks.

Michael
UK

Code:
<html>
<body>
<font face="Verdana, Arial, Helvetica, sans-serif" size="1">
<?php 
mysql_connect (localhost, eyeb, eye28);
mysql_select_db (eyeb);

if ($Type == "")
{$Type = '%';}

if ($Name == "")
{$Name = '%';}

$result = mysql_query ("SELECT * FROM Tourism
                         WHERE Type LIKE '$Type%'
                         AND Name LIKE '$Name%'
                       ");

if ($row = mysql_fetch_array($result)) {

do {
  print ("<i>");  
  print $row["Type"];
  print ("</i>");
  print ("<BR>");
  print ("<B>");
  print $row["Name"];
  print ("</B>");
  print ("<BR>");
  print $row["Description"];
  print ("<BR>");
  print ("For booking call Tourist Information on (01271) 870553");
  print ("<P>");
} while($row = mysql_fetch_array($result));

} else {print "Sorry, no records were found!";}

?>
</font>
</body>
</html>

[Edit by swirlee: Added code tags. They're there for a reason, people -- use them!]
 
You need to have two %'s. So it will look something like this:

SELECT * FROM tableName WHERE fieldName LIKE '%bob%'

That will return any record where "fieldName" contains bob anywhere in the string.
 
Cool

Thanks for that. It helped me out with my little coding dilemma. All I needed was a % before the string variable and it solved my problem.

Code:
%$SearchName%

Thanks for answering rally1484's original question, elitecodex. It certainly helped me.
 
Hi,

I would recommend you to create a fulltext index and then use

select * from table where match ... against...
 
Back
Top