Using Boolean Fulltext Searching with Multiple Keywords

Hello,
I am making an online exam with textboxes. If the user enters an answer with the correct keyword or keywords, then he will be told "You are correct!" and of course "You are wrong" if the opposite is true...
It was recommended to me to use the following code:

$query="SELECT
MATCH (q1) AGAINST ('$uno' IN BOOLEAN MODE) as ans1,
MATCH (q2) AGAINST ('$dos' IN BOOLEAN MODE) as ans2,
MATCH (q3) AGAINST ('$tres' IN BOOLEAN MODE) as ans3
 FROM formone_ex1_secthreesr";


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



mysql_close();



if($data["ans1"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $uno</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $uno</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>Yo me llamo John Smith</b>.\"</font></p>";
}

if($data["ans2"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $dos</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $dos</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; hora es?</b>\"</font></p>";
}

if($data["ans3"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $tres</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $tres</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; fecha es?</b>\"</font></p>";
}



However, even if I eneter an incorrect keyword, not contained within the database, it is returned as correct.
Can anyone help me fix this please?
ximenaoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shinesh PremrajanEngineering ManagerCommented:

I think you are missing this in the condition

Give a try on this

if($data["ans1"]!='0' && $data["ans1"]!='')

apply same for all the answers..

Hope this helps
0
ximenaoAuthor Commented:
Thanks once again for responding shinuq.

Now all the answers come up wrong, even if the keyword is actually in the database.
0
Shinesh PremrajanEngineering ManagerCommented:
Could you please paste the code here.. i am expecting some typo in the code.

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ximenaoAuthor Commented:
You may be right, but I just can't see it.
Below is the whole thing.
<?php

	// database connection //
	$db_host = 'localhost';
	$db_user = 'db_user';
	$db_pwd = 'db_pwd';
	$db_connect = mysql_connect($db_host,$db_user,$db_pwd) or die(mysql_error());
$db_name = "db_name"; 
mysql_select_db($db_name);
	
	?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Obtener Respuestas</title>
</head>

<body>

<?php

$uno = $_POST['uno'];
  $dos = $_POST['dos'];
  $tres = $_POST['tres'];
  $cuatro = $_POST['cuatro'];
  $cinco = $_POST['cinco'];
  
$query="SELECT 
MATCH (q1) AGAINST ('$uno' IN BOOLEAN MODE) as ans1,
MATCH (q2) AGAINST ('$dos' IN BOOLEAN MODE) as ans2,
MATCH (q3) AGAINST ('$tres' IN BOOLEAN MODE) as ans3,
MATCH (q4) AGAINST ('$cuatro' IN BOOLEAN MODE) as ans4,
MATCH (q5) AGAINST ('$cinco' IN BOOLEAN MODE) as ans5
 FROM formone_ex1_secthreesr";
$data=mysql_query($query) or die(mysql_error()); 



mysql_close();



if($data["ans1"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $uno</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $uno</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>Yo me llamo John Smith</b>.\"</font></p>";
}



if($data["ans2"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $dos</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $dos</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; hora es?</b>\"</font></p>";
}



if($data["ans3"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $tres</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $tres</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; fecha es?</b>\"</font></p>";
}



if($data["ans4"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $cuatro</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $cuatro</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>Soy hijo &uacute;nico</b>\"</font></p>";
}



if($data["ans5"]!='0') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $cinco</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $cinco</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>corto delgado y blanco</b>\"</font></p>";
}

?>


</body>
</html>

Open in new window

0
Shinesh PremrajanEngineering ManagerCommented:
Just give a try on these, this is suppose to resolve your query.
<?php

        // database connection //
        $db_host = 'localhost';
        $db_user = 'db_user';
        $db_pwd = 'db_pwd';
        $db_connect = mysql_connect($db_host,$db_user,$db_pwd) or die(mysql_error());
$db_name = "db_name"; 
mysql_select_db($db_name);
        
        ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Obtener Respuestas</title>
</head>

<body>

<?php

$uno = $_POST['uno'];
  $dos = $_POST['dos'];
  $tres = $_POST['tres'];
  $cuatro = $_POST['cuatro'];
  $cinco = $_POST['cinco'];
  
$query="SELECT 
MATCH (q1) AGAINST ('$uno' IN BOOLEAN MODE) as ans1,
MATCH (q2) AGAINST ('$dos' IN BOOLEAN MODE) as ans2,
MATCH (q3) AGAINST ('$tres' IN BOOLEAN MODE) as ans3,
MATCH (q4) AGAINST ('$cuatro' IN BOOLEAN MODE) as ans4,
MATCH (q5) AGAINST ('$cinco' IN BOOLEAN MODE) as ans5
 FROM formone_ex1_secthreesr";
$data=mysql_query($query) or die(mysql_error()); 



mysql_close();



if($data["ans1"]!='0' && $data["ans1"]!='') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $uno</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $uno</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>Yo me llamo John Smith</b>.\"</font></p>";
}



if($data["ans2"]!='0' && $data["ans2"]!='') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $dos</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $dos</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; hora es?</b>\"</font></p>";
}



if($data["ans3"]!='0' && $data["ans3"]!='') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $tres</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $tres</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>&iquest;Qu&eacute; fecha es?</b>\"</font></p>";
}



if($data["ans4"]!='0' && $data["ans4"]!='') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $cuatro</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $cuatro</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>Soy hijo &uacute;nico</b>\"</font></p>";
}



if($data["ans5"]!='0' && $data["ans5"]!='') {
        echo "<p><font color=\"#7E4B01\" size=\"+1\"><b> $cinco</b></font> is correct!</p>";
} else {
        echo "<p>Sorry,<font color=\"#FF0000\" size=\"+1\"><b> $cinco</b></font> is wrong! The correct answer is <font color=\"#7E4B01\" size=\"+1\">\"<b>corto delgado y blanco</b>\"</font></p>";
}

?>

</body>
</html>

Open in new window

0
ximenaoAuthor Commented:
Once again, thanx alot 4 helping me shinuq.
What I get now is every answer 'wrong', even if the entered answer is actually correct.

Interestingly, once I tried removing "IN BOOLEAN MODE" just to see what would happen, and I get a error saying "Can't find FULLTEXT index matching the column list query:"
Does this mean that there is something wrong with my sql table?

I used the attached CREATE TABLE query and modified each field (except for 'id') with this code:
ALTER TABLE formone_ex1_secthreesr ADD FULLTEXT(q1) and then for the next field I would replace 'q1' with 'q2' and so on.
CREATE TABLE formone_ex1_secthreesr (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`q1` TEXT( 75 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`q2` TEXT( 75 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`q3` TEXT( 75 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`q4` TEXT( 75 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`q5` TEXT( 75 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL 
)

Open in new window

0
Shinesh PremrajanEngineering ManagerCommented:
Please place a debugger in your code, just to see what the database actually returns.
I guess there is something actually not returned by the database.

Let me know..

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

mysql_close();

echo "<pre>";
print_r($data);
echo "</pre>";

Open in new window

0
ximenaoAuthor Commented:
It returns "Resource id #2".
0
Shinesh PremrajanEngineering ManagerCommented:
good problem traced.. Its like, we are getting the resource identifier and not the actual table data.

Pls update the below code


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

$data=mysql_fetch_array($res);

mysql_close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ximenaoAuthor Commented:
shinuq knows his stuff; although sometimes I wish this expert wouldn't assume that everyone has that knowledge of PHP/MySQL.
0
ximenaoAuthor Commented:
OK that did it! Thanks for your persistence with this problem, shinuq. Now I'll definitely subscribe to Experts Exchange.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.