Okay, so I'm trying to do a random question and answer php coding using a database to store each question, answer and ID.
So far I've managed to get the random question, using this code:
<?php
//select database for use
mysql_select_db ('carlott_ravie') or die ('Unable to select database!');
//create and exectue query
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `questions` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `questions` LIMIT $offset, 1 " );
//check if records were returned
if (mysql_num_rows($result) > 0)
{
//print HTML
//iterate over record set
// print each field
while($row = mysql_fetch_row($result))
{ echo '' . $row[1] . '';
echo '</div></center>';
}
}
else
{
//print error message
echo 'No events in planning.';
}
//once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection)
?>
What I'm wanting to do further than that, is have the user enter the answer and check it against the stored answer in the database. If it's right the user gets a button or redirected to another page, if it's wrong, the user gets redirected to a different page telling them it's wrong.
row[2] is where the answers are in the database, so I'm figuring since it's random, I'd have to load the question into a variable in the same php session, but my attempts have been unsuccessful.
I have no idea how to check the user's answer is the same as the answer stored. Any help would be greatly appreciated.