Re: query not returnung rows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I cannot beleive your using LIKE - this is the most intensive way of 
doing a search you can possibly write with SQL. It is also slow and 
pretty rubbish, you can't search multiple fields effectivly and for alot 
of searches your server is going to crash!

Use a fulltext index on a new table called search_text, sync the columns 
you want to search in e.g username, about me, title, email and everytime 
the script updates these columns in the original table have it updated 
in the new search_text

Then use a MATCH column AGAINST('search for this) and see queries that 
execute in 0.00002 seconds rather then 2 seconds and your server will 
not crash

your results will also be relevant as you can sort by relevence by using 
something like this

SELECT table.columnName, table2.columnName MATCH( search_text.title, 
search_text.description, search_text.keyword ) AGAINST ('". 
$search_variable ."') AS relevance FROM table, table2 WHERE table.id = 
table2.uid AND table.active='1' AND table2.verified='1' AND MATCH( 
search_text.title, search_text.description, search_text.keyword ) 
AGAINST ('". $search_variable ."' IN BOOLEAN MODE ) HAVING relevance > 0.2

:)

DO NOT USE LIKE!! IT'S RUBBISH AND OLD AND LIMITED

malayappa2000 wrote:
>
> Hi Guys fallowing is code for search form. Here what the problem is
> the query
> SELECT * FROM bulk_register WHERE ".$field." LIKE'%".$find."%'";
> not returning any Result.
> plz tell me whre the error is.
>
> <?php
> if(isset($_POST['search']))
> {
> echo $_POST['field'];
> $find=$_POST['find'];
> $field=$_POST['field'];
> //This is only displayed if they have submitted the form
> if (isset($_POST['searching']))
> {
> echo "<h2>Results</h2><p>";
>
> //If they did not enter a search term we give them an error
> if ($find == "")
> {
> echo "<p>You forgot to enter a search term";
> exit;
> }
>
> // Otherwise we connect to our Database
> mysql_connect("localhost", "root", "") or die(mysql_error());
> mysql_select_db("bulksms") or die(mysql_error());
>
> // We preform a bit of filtering
> //$find = strtoupper($find);
> //$find = strip_tags($find);
> $find = trim ($find);
> echo "find is".$find."<br>";
> //Now we search for our search term, in the field the user specified
> $query="SELECT * FROM bulk_register WHERE ".$field." LIKE'%".$find."%'";
> echo "query is".$query;
> $data = mysql_query($query);
>
> //And we display the results
> while($result = mysql_fetch_array( $data ))
> {
> echo $result['firstname'];
> echo " ";
> echo $result['lastname'];
> echo "<br>";
> echo $result['mobile'];
> echo "<br>";
> echo "<br>";
> }
>
> //This counts the number or results - and if there wasn't any it gives
> them a little message explaining that
> $anymatches=mysql_num_rows($data);
> if ($anymatches == 0)
> {
> echo "Sorry, but we can not find an entry to match your query<br><br>";
> }
>
> //And we remind them what they searched for
> echo "<b>Searched For:</b> " .$find;
> }
>
> }
> ?>
>
> <h2>Search</h2>
> <form name="search" method="post" action="searchform.php">
> Seach for: <input type="text" name="find" /> in
> <Select NAME="field">
> <Option VALUE="firstname">First Name</option>
> <Option VALUE="lastname">Last Name</option>
> <Option VALUE="mobile">Mobile</option>
> </Select>
> <input type="hidden" name="searching" value="yes" />
> <input type="submit" name="search" value="Search" />
> </form>
>
>  


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux