Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:41:28+00:00 2026-05-25T06:41:28+00:00

I have a script below which reads on displayed information from my mysql database

  • 0

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/>';
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:41:29+00:00Added an answer on May 25, 2026 at 6:41 am

    Let’s say for instance you have an array $results which 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_replace to achieve this:

    foreach($results as $result)
        echo str_replace($search,'<b>'.$search.'</b>',$result['name']);
    

    This replaces all instances of $search (which should be your search string) with <b>$search</b> within $result['name'].

    In your case:

    while ($row = mysql_fetch_array($sql)){
    
        echo '<br/> Category: '.str_replace($term,'<b>'.$term.'</b>',$row['category']);
        echo '<br/> Title:    '.str_replace($term,'<b>'.$term.'</b>',$row['title']);
        echo '<br/> Address:  '.$row['add1'];
        echo '<br/> Street:   '.$row['street'];
        echo '<br/> City:     '.$row['city'];
        echo '<br/> Postcode: '.str_replace($term,'<b>'.$term.'</b>',$row['postcode']);
        echo '<br/> Phone:    '.$row['phone'];
        echo '<br/> E-Mail:   '.$row['email'];
        echo '<br/> Website:  '.$row['website'];
        echo '<br/><br/>';
    
    }
    

    BY THE WAY (IMPORTANT)

    What you are doing here:

    $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%' ");
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file download script that I have written, which reads files from
I have a user who randomly gets a Script Error box (shown below) which
I have tested the below script on a demo page which is not using
I have a Perl script which nests foreach loops as seen below. It takes
I have a python script which outputs lots of data, sample is as below.
I have a small script which im using to test PHP mail(), as below:
I've an annoying ASP.NET problem: I have a Perl script (see below), which gets
I have the script below which works well, i.e. it generates a chart on
Hope someone can help. I have the code below which when the user hovers
I have a NAnt script like below: <if test=${a}>${b}> <call target=target/> </if> What I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.