Jump to content

Recommended Posts

Hello all,

 

I am having a small problem with getting the following code working... I am sure its an easy problem, just need another set of eyes on it..

THANKS for any help! Scott P.

 

if ($search == NULL or $search == ‘%’){

} else {

for ($i=0; $i<count($keywords); $i++) {

$query = "

SELECT link, description, title

FROM search_data

WHERE keyword LIKE \"%$keywords[$i]%\"  <--- I think the problem is in here somewhere...but may not be..

ORDER BY link";

}

 

Thanks Again.

Link to comment
https://forums.phpfreaks.com/topic/94402-php-and-sql-search-engine-script/
Share on other sites

Here is more of the script, it is different then the first one that I posted as I have been working on it.. but it still isn't working properly ...

 

The data from the POST is from a form of a search engine... any help would be great, thanks

 

$search_txt = $_POST['search_text'];

 

$search = $search_txt;

 

$keywords = explode(" ", $search);

 

$keywords = array_diff($keywords, array(""));

 

 

include('dbcreds.php');

 

 

 

 

if($search == NULL or $search == ‘%’){

}

else{

 

 

$query = "SELECT * FROM search_data WHERE";

 

for ($i=0; $i<count($keywords); $i++) {

 

$query = "keyword LIKE  '%" . $keywords[$i] . "%';

}

 

 

 

 

$result = mysql_query($query) or die(mysql_error());

 

 

if ($search == NULL or $search == '%'){

} else {

$count = mysql_num_rows($result);

$total_results = $count;

}

 

when your query is executed using the mysql_query() function it is missing the "SELECT * FROM search_data WHERE" because you are reassigning the contents of $query rather than appending to it.

 

For example:

 

$sql = "SELECT * FROM search_data WHERE";

//append the where clause

$sql .= " keyword = 'whatever'";

 

The $sql string now = "SELECT * FROM search_data WHERE keyword = 'whatever'"

 

.....essentially what you have is the following:

 

$sql = "SELECT * FROM search_data WHERE";

$sql = " keyword = 'whatever'";

 

The $sql string now = " keyword = 'whatever'"

 

;)

 

 

  • 2 weeks later...

Looking a bit closer at the following code, you would need to have an "OR" statement if more than one results was returned from the original query

 

for ($i=0; $i<count($keywords); $i++) {
  $query = "keyword LIKE  '%" . $keywords[$i] . "%';
}

 

For example:

 

select * from search_data
where (keyword like '%this%'
or keyword like '%that%')

 

The above example demonstrates how the sql statement would need to look like if 2 keywords were provided on the search screen

 

:)

 

Try something like this:

 

        $query = "SELECT * FROM search_data WHERE";

        $sqlOR = " ";

        for ($i=0; $i<count($keywords); $i++) {

                $query .= $sqlOR . "keyword LIKE  '%" . $keywords[$i] . "%';

                $sqlOR = " or  ";

        }

 

note the use of .= (dot/equals) to append each keyword so as not to destroy the original select command and the use of the $sqlOR string. This is space to start with and set to ' or ' for subsequent keywords so you get keyword like '%keyword1%' or keyword like '%keyword2%'.

 

personally I would replace the for loop with a foreach

 

i.e. foreach ($keywords as $keyword)

 

Good luck.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.