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
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%'

Link to comment
Share on other sites

But it wont find it because the field in the mysql database is called question which has the 'what is vb' question in and php can only find it if it is the full string not just words from the string

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.