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

  • Home
  • SEARCH
  • 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 5848511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:52:09+00:00 2026-05-22T12:52:09+00:00

I’m having trouble with my search script. Basically, all is fine with the search

  • 0

I’m having trouble with my search script.

Basically, all is fine with the search if the result is found but if there are no matches in the DB(MySQL) then my error doesn’t display.. am i missing something? heres the code:

<?php
$term = $_GET['term'];
$sql = mysql_query("select * from search_e where content like '%$term%'");
while ($row = mysql_fetch_array($sql)){ 
$data = $row['content'];
$first_pos = strpos($data,$term);
if ($first_pos !== false) {
                  $output = substr($data,max(0,$first_pos - 100),200 + strlen($term));?>


<div>
<p class="ptitle"><?php echo $row["fn"]; ?></p><hr>
            Brief summary of contents:
            <hr class="hr">
            <p style="padding: 5px;">
        <i>"<?php echo $output; ?>" </i>..
            </p>


</div><br><br>
<?php
}
else  {?>
<div><?php echo "Sorry! No results were found using term: ".$_GET['term']."<br>Try using fewer Keywords"; ?></div>
<?php }?>
<?php
}
//close
    mysql_close();

?>

This may be something simple im doing wrong but i just cant figure it out. Also i know the code is dirty but its how i work.

I was also hoping to implement a little snippet i found browsing the net, which higlights specific words in a phrase.

function highlight($sString, $aWords) {
    if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
        return false;
    }

    $sWords = implode ('|', $aWords);
    return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}

Is this possible to work into my script??

  • 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-22T12:52:10+00:00Added an answer on May 22, 2026 at 12:52 pm

    If I’m clear about what you’re trying to accomplish, I would change it like so:

    if(mysql_num_rows($sql) > 0) {
        while ($row = mysql_fetch_array($sql)) { 
            ...
        }
    } else {
        echo("No Records!");
    }
    

    And barfoon is correct. Protect your web site and backend database from malicious users.

    $term = mysql_real_escape_string($_GET['term']);
    

    Edit

    For completeness, after looking back over what you posted the reason you are getting no output is because if no matches are found anything inside of the while loop will not be executed, so your if($first_pos !== false) check is meaningless, except as a sort of ‘sanity check’ for records that did match.

    To highlight the words using the function you posted, change:

    <i>"<?php echo $output; ?>" </i>
    

    To:

    <i>"<?php echo highlight($output, array( $term )); ?>" </i>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.