需要帮助使用php和mysql制作搜索功能

需要帮助使用php和mysql制作搜索功能

问题描述:

I've made a search function using php and mysql. I'm using this

$raw_results = mysql_query("SELECT * FROM entries
            WHERE (`title` LIKE '%".$query."%') OR (`contents` LIKE '%".$query."%')") or die(mysql_error());

And then I'm echoing it like this

while($results = mysql_fetch_array($raw_results)){
echo "<p><a href=index.php?id=".$results['id']."> <h3>".$results['title']."</h3></a>".$results['contents']."</p>";
}

This shows the title and content. But I want to echo only two lines from the 'content' which contains the query word rather than echoing whole paragraph. Can anyone help me here ?

It can be easily achieved with the help of substr php function. Syntax : substr(string,start,length)

Following example will print half of the string from content column.

Referring to your code :

    while($results = mysql_fetch_array($raw_results)){
   echo "<p><a href=index.php?id=".$results['id']."> <h3>".$results['title']."</h3></a>".substr($results['contents'], strpos($results['contents'], $query), 20)."</p>";
    }

Refer below linkenter link description here