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 6124815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:10:34+00:00 2026-05-23T16:10:34+00:00

I have a PHP search script that queries a MySQL database and then parses

  • 0

I have a PHP search script that queries a MySQL database and then parses the results through HTML to allow CSS styling. I want the script to highlight all of the keywords in the results that the user has search for. How can I do this with PHP?

My PHP script is:

<?php

mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!empty($_GET['q'])){
$query=mysql_real_escape_string(trim($_GET['q']));
$searchSQL="SELECT * FROM links WHERE `title` LIKE '%{$query}%'  LIMIT 8";
$searchResult=mysql_query($searchSQL);

while ($row=mysql_fetch_assoc($searchResult)){
    $results[]="<a href='{$row['url']}' class='webresult'><div class='title'>{$row['title']}</div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></a>";
}

if(empty($results)){
echo 'No results were found';
} else {
echo implode($results);
}
}

?>
  • 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-23T16:10:34+00:00Added an answer on May 23, 2026 at 4:10 pm

    Simplistically you could adapt the loop here:

    $searchvar = trim($_GET['q']);
    while ($row=mysql_fetch_assoc($searchResult)){
        $description = str_replace($searchvar, '<span class="highlight">'.$searchvar."</span>", $row['description']);
        $results .="<a href='{$row['url']}' class='webresult'>
        <div class='title'>{$row['title']}</div>
        <div class='desc'>{$description}</div>
        <div class='url'>{$row['url']}</div></a>";
    }
    

    To make it a little better:

    $searchvar = explode(" ", trim($_GET['q'])); //puts each space separated word into the array.      
    while ($row=mysql_fetch_assoc($searchResult)){
        $description = $row['description'];
        foreach($searchvar as $var) $description = str_replace($var, '<span class="highlight">'.$var."</span>", $description);
        $description = str_replace($searchvar, '<span class="highlight">'.$searchvar."</span>", $row['description']);
        $results .="<a href='{$row['url']}' class='webresult'>
        <div class='title'>{$row['title']}</div>
        <div class='desc'>{$description}</div>
        <div class='url'>{$row['url']}</div></a>";
    }
    

    The benefit of the second one there is that if a user types in “ipod toudch yellow” you will be searching for “ipod”, “toudch” and “yellow” which would negate the type and make the results more general.

    You would need to exchange the single:

    like '%query%'
    

    with

    foreach(explode(" ", trim($_GET['q']) as $searchvar) $where[] = "like '%$searchvar%'";
    $wheresql = implode(" OR ", $where);
    

    to get each search “word” to be looked for in the sql or you will have a limited search with a unrelated highlight.

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

Sidebar

Related Questions

I have a PHP script that builds a binary search tree over a rather
I have a PHP script that's supposed to take in a URL and search
I have a PHP script that runs as a CGI program and the HTTP
I need to modify a php search script so that it can handle multiple
I have PHP script that fetch messages from a mailbox. I use the imap_search
I have a Google Instant style search script written in jQuery which queries a
I have a jquery script that searches a php file that produces an XML
I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/ Like
I have PHP configured so that magic quotes are on and register globals are
We have PHP 5.2.6 deployed to c:\php and in that folder there is the

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.