Jump to content

Searching Databases


-Karl-

Recommended Posts

Hello,

 

I have a simple script I've coded:

   // Collect skills
   $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$_GET['search']}%' OR `guidename` LIKE '%{$_GET['search']}%' OR `introduction` LIKE '%{$_GET['search']}%' OR `content` LIKE '%{$_GET['search']}%' OR `guide` LIKE '%{$_GET['search']}%')";
   $runskills = mysql_query($skillsquery);
if(!mysql_num_rows($runskills)==0){
   if($runskills){
      while($arr = mysql_fetch_assoc($runskills)){
if(mysql_num_rows($runskills) > 0){
         echo <<<HTML
<font color="#239dd9"><b>{$arr['**']}</b></font><br/>
<font color="#a0a7ae">{$arr['**']}</font><br/>
<a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/>
HTML;
      } 
    }
  }
} 

 

This will search for a keyword placed by a visitor and display any matches.

 

However, if someone searches for two or more keywords it won't display an article if the two keywords are not next to each other in the field within the database.

 

For example, if a field contains "hello world how are you?" and a visitor searches for "hello how" it won't display it, as it searches for %hello how%. This is how I intended it to be, however, it makes searching less accurate.

 

I was wondering if there was a way to do this, by exploding the keywords that a visitor searches. Such as if they search for "hello how" it will search for a field which contains both "hello" AND "how".

 

Hope I've explained this clear enough.

 

Any help and pointers are appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/196262-searching-databases/
Share on other sites

Okay, I have the exploding done:

$searchquery = $_GET['search'];
$explode = explode(" ", $searchquery);

echo $explode[0];
echo $explode[1];

 

However, how would I make this work in a MySQL query if there is 2 or more keywords to search for ($explode[0], $explode[1], $explode[2], etc)?

 

How would I do a foreach?

Link to comment
https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030720
Share on other sites

Okay so I have:

foreach ($explode as $exploded) {
   // Collect skills
   $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$exploded}%' OR `guidename` LIKE '%{$exploded}%' OR `introduction` LIKE '%{$exploded}%' OR `content` LIKE '%{$exploded}%' OR `guide` LIKE '%{$exploded}%')";
   $runskills = mysql_query($skillsquery);
if(!mysql_num_rows($runskills)==0){
   if($runskills){
      while($arr = mysql_fetch_assoc($runskills)){
if(mysql_num_rows($runskills) > 0){
         echo <<<HTML
<font color="#239dd9"><b>{$arr['guidename']}</b></font><br/>
<font color="#a0a7ae">{$arr['introduction']}</font><br/>
<a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/>
HTML;
}
      } 
    }
  }
} 

 

But this echo's the same thing twice, if you search for "hello you" and both words are found in the same field, it will echo it twice. How could I combat this?

Link to comment
https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030727
Share on other sites

Just in case someone else is looking how to do this, I'll post how I fixed it.

 

   // Collect skills
foreach ($explode as $exploded) {
   $skillsquery = "SELECT * FROM `skills` WHERE (`id` LIKE '%{$exploded}%' OR `guidename` LIKE '%{$exploded}%' OR `introduction` LIKE '%{$exploded}%' OR `content` LIKE '%{$exploded}%' OR `guide` LIKE '%{$exploded}%')";
   $runskills = mysql_query($skillsquery);
  }
if(!mysql_num_rows($runskills)==0){
   if($runskills){
      while($arr = mysql_fetch_assoc($runskills)){
if(mysql_num_rows($runskills) > 0){
         echo <<<HTML
<font color="#239dd9"><b>{$arr['guidename']}</b></font><br/>
<font color="#a0a7ae">{$arr['introduction']}</font><br/>
<a href="./***.php?id={$arr['id']}">***?id={$arr['id']}</a><br/><br/><br/>
HTML;
}
      } 
    }
} 

Link to comment
https://forums.phpfreaks.com/topic/196262-searching-databases/#findComment-1030728
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.