Jump to content

[SOLVED] PHP Search Engine


H

Recommended Posts

Hello, I am trying to make a website which has a search engine what can answer poeples questions, the only problem is they have to type the question exactly eg. 'what is vb' (shows the answer) 'what's vb' (does not show the answer). Is there any way I can make it search for the keywords so 'what' and 'vb' would show the answer without editing my code completely.

 

HERES MY CODE!

 

<p><font color="#999999"><span style="font-size: 15pt">Question Asked<br>
</span></font><font size="2">
<?php
$question = addslashes($_POST['question']);
echo $question;
?>
</font></p>
<p><font color="#999999"><span style="font-size: 15pt">Answer<br>
</span></font><font size="2">
<?php
$host = 'localhost';
$user = '?';
$pass = '?';
$db = '?';
$connect = mysql_pconnect($host, $user, $pass);
$selectdb = mysql_select_db($db);

mysql_connect($host, $user, $pass);
mysql_select_db($db);
$SQLq = "SELECT * FROM `search` WHERE `question` LIKE '$question'";
$sql = mysql_query($SQLq)or die(mysql_error()); 
$fetch = mysql_fetch_array($sql);
$answer = $fetch['answer'];
if($answer == ""){
echo 'Sorry, I dont know the answer to that one.';
} else {
echo $answer;
}
?> 
</font></p>

 

NOTE: THE USERNAMES ETC HAVE BEEN REMOVED FROM THE CODE

Link to comment
https://forums.phpfreaks.com/topic/53271-solved-php-search-engine/
Share on other sites

if you look at my last post, a few tweak and you should beable to get it working..

 

basically you final SQL query should be something like this

 

$SQLq = "SELECT * FROM `search` WHERE
`question` LIKE '%what%' AND 
`question` LIKE '%is%' AND 
`question` LIKE '%vb%' 
";

 

so you need to alter the basic script i wrote from

what OR is OR vb

to

`question` LIKE '%what%' AND `question` LIKE '%is%' AND `question` LIKE '%vb%'

No..

"SELECT * FROM `search` WHERE `question` = 'what'

will only find record(s) which ONLY have 'what' in the question field

 

if you do

"SELECT * FROM `search` WHERE `question` LIKE '%what%'

it will find all that have 'what' anywhere in the field question..

 

heres the code i would use

 

<?php
$search = "what is vb";
$SQLq = "SELECT * FROM `search` WHERE ";

$parts= explode(" ", $search);
$start = "`question` LIKE '%";
$middle = "%' AND `question` LIKE '%";
$end = "%'";
$search = $start.implode($middle, $parts).$end;

echo $SQLq.$search;

$SQLq = $SQLq.$search;
?>

 

NOTE it needs cleaning up (ie search on 0/1 word) will fail

just a quick one (on the phone so kinda hard to do)

 

<p><font color="#999999"><span style="font-size: 15pt">Question Asked<br>
</span></font><font size="2">
<?php
$question = addslashes($_POST['question']);
echo $question;
?>
</font></p>
<p><font color="#999999"><span style="font-size: 15pt">Answer<br>
</span></font><font size="2">
<?php
$host = 'localhost';
$user = '?';
$pass = '?';
$db = '?';
$connect = mysql_pconnect($host, $user, $pass);
$selectdb = mysql_select_db($db);

mysql_connect($host, $user, $pass);
mysql_select_db($db);

$SQLq = "SELECT * FROM `search` WHERE ";

$parts= explode(" ", $question);
$start = "`question` LIKE '%";
$middle = "%' AND `question` LIKE '%";
$end = "%'";
$search = $start.implode($middle, $parts).$end;

echo $SQLq.$search;

$SQLq = $SQLq.$search;

//$SQLq = "SELECT * FROM `search` WHERE `question` LIKE '$question'";

$sql = mysql_query($SQLq)or die(mysql_error()); 
$fetch = mysql_fetch_array($sql);
$answer = $fetch['answer'];
if($answer == ""){
echo 'Sorry, I dont know the answer to that one.';
} else {
echo $answer;
}
?> 
</font></p>

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.