Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 199903

Re: [PHP3] 2 words search From: Adam Gotheridge (adam <email protected>)
Date: 03/28/99

Try constructing the where clause of the sql statement first and then
execute 1 query to return the information you want. Once you have created
your sql statement, echo the statement to see if it is valid (you can then
run the query on something else if you wish). For instance:

<?
 $words = explode (" ", $search);
 $num = COUNT ($words);

$SQLstr = "select * from authors ";

WHILE ($n < $num) {
    $SQLWhere = $SQLWhere ."title like '%". $words[$n] ."%'";
    if ($n != $num)

        $SQLWhere = $SQLWhere ." or ";
    }
}

$Whole = $SQLstr + $SQLWhere;

echo ("SQLstr = $SQLstr<br>\n");
echo ("SQLWhere = $SQLWhere <br>\n");
echo ("Whole = $Whole <br>\n");
?>

I could have this all goofed up, but it should be easier to figure out what
is going on this way and more efficient too. If you want to, you test for
words like "and" and use that to help construct the statement.

Adam

> Hello, I have a problem with a query. I'd like to make a search in a
> table inserting 2 or more words in the input form, using the OR.
> Everything works fine with 1 word but with more I obtain result only for
> the LAST inserted one.
>
> WHY ?
>
> How can I build it? This is my code:
> $words = explode (" ", $search);
> $num = COUNT ($words);
> WHILE ($n < $num):
> $query = "SELECT * FROM newsWHERE (title LIKE '%$words[$n]%') OR (text
> LIKE '%$words[$n]%')ORDER BY id DESC";
> $result = MYSQL_QUERY($query);
> $n++;
> ENDWHILE;
>
>
> Thanks!
> Valerio

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3
List administrator:  zeev-list-admin <email protected>