In need of Php simple free text search script

Asked By 0 points N/A Posted on -
qa-featured

I'm trying to make a basic text search engine using MySQL/PHP with the script <? PHP $text ="Hello World!"; if ($text contains "world") {echo "True";}? >. 

Why is it that I'm having an error of "Fatal error: Call to undefined function empty () in /home/h's/public_html/sign/searchtest.php on line 60"? 

I need PHP simple free text search script. 

Thanks.

SHARE
Best Answer by Drake Vivian
Answered By 0 points N/A #166623

In need of Php simple free text search script

qa-featured

Hi Groat,

This can be easily done using PHP string functions like following:

strpos: Find the position of the first occurrence of a substring in a string

strstr:Find the first occurrence of a string

strpbrk: Search a string for any of a set of characters

strcmp : Binary safe string comparison

<?php

$mystring = 'abc';

$findme   = 'a';

$pos = strpos($mystring, $findme);

if ($pos === false) {

    echo "The string '$findme' was not found in the string '$mystring'";

} else {

    echo "The string '$findme' was found in the string '$mystring'";

    echo " and exists at position $pos";

}

?>

Thanks.

Best Answer
Best Answer
Answered By 30 points N/A #166625

In need of Php simple free text search script

qa-featured
Hi there Groat N,
 
Since I don't see your code I can't give you the answer why you are getting the mentioned error, but can give you the working sample.
 
I've attached two pieces of code that will help you. The first one is a simple search form that will show the search box on your page, and the second one is the search process that will give you the search results. Copy and paste both codes in your PHP document.
 
You will need to adjust the names of your files, database and database access info in order to make things working.
 
Best regards,
Drake Vivian
 
Search form:
 
<form method="post" action="search.php">
<input type="text" value="Search…" name="query" />
<input type="submit" value="Find" name="completedsearch" />
</form>
 
Process: 
 
<?php
                        if(isset($_POST['completedsearch']))
                        {
                                $term = $_POST['query'];
                                $mysql = mysql_connect("localhost","yourusername","yourpassword");
                                mysql_select_db("yourdatabase");
                                $qu = mysql_query("SELECT * FROM your_table WHERE condition1 LIKE '%{$term}%' OR condition2 LIKE '%{$term}%' OR condition3 LIKE '%{$term}%' OR condition4 LIKE '%{$term}%' "); //selects the row that contains ANYTHING like the submitted string
                                echo "
                                                <th>Condition 1 Results</th>
                                                <th>Condition 2 Results</th>
                                                <th>Condition 3 Results</th>
                                                             <th>Condition 4 Results</th>
                                                ";
                                while($row = mysql_fetch_array($qu))
                                           {
 
                                                echo "<tr><td>";  
                                                echo $row['condition1'];
                                                echo "</td>";
                                                echo "<td>";
                                                echo $row['condition2'];
                                                echo "</td>";
                                                echo "<td>";
                                                echo $row['condition3'];
                                                                                                                   echo "</td><td>";
                                                                                                                   echo $row['condition4'];
                                                        echo "</tr></td>";
                                }
                        }
                ?>
 

 

Related Questions