• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

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?
0
ximenao
Asked:
ximenao
  • 6
  • 5
1 Solution
 
Shinesh PremrajanTechnical 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 PremrajanTechnical ManagerCommented:
Could you please paste the code here.. i am expecting some typo in the code.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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 PremrajanTechnical 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 PremrajanTechnical 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 PremrajanTechnical 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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now