I have a script below which reads on displayed information from my mysql database on my webpage, how can I make matched results bold please? for example, if I searched “john” how to make the displayed results “john bloggs”. thanks
here is the script so far,
<?
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("databasename");
$term = $_POST['term'];
$sql = mysql_query("select * from tablename where category like '%$term%' or title like '%$term%' or postcode like '%$term%' or info like '%$term%' ");
while ($row = mysql_fetch_array($sql)){
echo '<br/> Category: '.$row['category'];
echo '<br/> Title: '.$row['title'];
echo '<br/> Address: '.$row['add1'];
echo '<br/> Street: '.$row['street'];
echo '<br/> City: '.$row['city'];
echo '<br/> Postcode: '.$row['postcode'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> E-Mail: '.$row['email'];
echo '<br/> Website: '.$row['website'];
echo '<br/><br/>';
}
?>
Let’s say for instance you have an array
$resultswhich contains a few results from your MySQL query.Let’s say you were searching within the field
name.You could use a very simple
str_replaceto achieve this:This replaces all instances of
$search(which should be your search string) with<b>$search</b>within$result['name'].In your case:
BY THE WAY (IMPORTANT)
What you are doing here:
Is extremely dangerous.
$_POST['term']comes from the user, what if this user fills in';DROP TABLE tablename --? Your query will suddenly change to something that will drop your entire table and delete all your information.You should always check your user input, here is a nice tutorial explaining some methods how:
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php