mysql and random comparison

Codes here !

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jul 16, 2012 12:42 pm

mysql and random comparison

Postby smithandson » Tue Jul 17, 2012 8:50 am

I have a table like the one below in mysql:
id word sentance answer explanation
1 happy I am sorry for your unhappines unhappiness
2 consume I am a consumer consumer

First I am trying to do is create the variable ‘$answer’ which captures in the random row generate , the content in the column "answer" of that row for exampls ‘unhappiness’

To generate the random I am using the code below:

if (!$con)

mysql_select_db("clients", $con);
$range_result = mysql_query( " SELECT MAX(`wd_id`) AS max_id , MIN(`wd_id`) AS min_id FROM `words` ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `words` WHERE `wd_id` >= $random LIMIT 0,1 ");


To echo the random the random answer I am using the following code.


while($row = mysql_fetch_array($result))
echo "<table border ='0' width='600px'>";
echo "<tr>";
echo "<td width='100px'> <font color='black'> Clue word: </font></td>";
echo "<td width='100px'>" . $row['wd'] . "</td>";
echo "<td width='150px'> <font color='black'>Context sentence: </font></td>";
echo "<td width='200px'>" . $row['st'] . "</td>";
echo "<td>" . $row ['wd_id'] . "</td>";
echo "<td>" . $row ['answer'] . "</td>";
echo "</tr>";


I also have a text input box and a submit button.
I want to compare the word typed in the input box with the random word.
If they are the same I want to echo the word in green
If not I want to echo the typed word in red
The code I have is as below:

To compare and green or red:

$input = $_POST['1'];

if (isset ($_POST['SUBMIT'])){

if ($input == $answer)

echo "<font color='green'>" . $answer . "</font> ";
else echo "<font color='red'>" . $input . "</font>";
echo $explanation;
echo $related;
echo $pron;


the problem I have is that
when I try comparing the input from the form to the variable $answer, it doesn't compare.

Basically because I are not able to define to variable $anwer with the information I want it to have.

I need the the code that defines the variable $answer (which would be content in the columna "answer" of the random row generated by the variable $result).
Any help would be very welcome

Many thanks

php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: mysql and random comparison

Postby johnj » Wed Jul 18, 2012 8:09 am

First of all , do not use words like "input, system, file , answer, question, date etc etc" as variable names.

second, if $input cannot be compared with $answer, then display both the variables and verify that they have a value.

Return to “mySQL & php coding”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest