Link to home
Start Free TrialLog in
Avatar of ximenao
ximenao

asked on

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?
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image


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
Avatar of ximenao
ximenao

ASKER

Thanks once again for responding shinuq.

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

Avatar of ximenao

ASKER

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

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

Avatar of ximenao

ASKER

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

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

Avatar of ximenao

ASKER

It returns "Resource id #2".
ASKER CERTIFIED SOLUTION
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ximenao

ASKER

shinuq knows his stuff; although sometimes I wish this expert wouldn't assume that everyone has that knowledge of PHP/MySQL.
Avatar of ximenao

ASKER

OK that did it! Thanks for your persistence with this problem, shinuq. Now I'll definitely subscribe to Experts Exchange.