Jump to content

Random questions from mysql


marcus88

Recommended Posts

Hello I am currently designing a web site for an on-line test with php and Mysql using apache server. I have to randomly pick questions from Mysql database via the server too displays on the main viewing screen. I would be grateful if you could point me in the right direction. Hope this makes sense because its starting to get me confuse I have just designed the main screen which generates the password with username.

 

Thanks

 

 

:?

Link to comment
https://forums.phpfreaks.com/topic/191-random-questions-from-mysql/
Share on other sites

I assume you have all questions stored in tables already.

 

For a random question select, do this query first:

 

SELECT MIN(id), MAX(id) FROM your_question_table 

 

I assume the ID is autoincremental, with no deleted records.

 

Then you have the minimum and maximum ID\'s. Use them for this:

 

srand((double)microtime()*1000000); 

$random_number = rand(0,100);

 

where 0 is your minimum and 100 your maximum...

Now select your random question:

 

SELECT question FROM your_question_table WHERE id = \'$random_number\'

 

There are many sources online on random strings and numbers, just search in google...

Thanks

 

I’m having a problem with my database I’m not to sure how many fields to put I have a main screen for the users to view and there will have a choice of four questions to chose from with one being the right answer. Will I have to create four separate fields with 4 wrong answers and one right and how will it no to generate the correct answer? That’s the problem I’m having its driving my mad.

 

:oops:

I think you mixed up some words like qestion and answer but let me try.

 

You only need to store the right answer.

 

I take it you use a drop down select option list , so all answers have a value. To see whether it was the right answer do this:

 

$query = "SELECT COUNT(*) FROM your_table WHERE correct_answer_field = $visitors_answer";

$result = mysql_query($query);

if(mysql_num_rows($result) < 1)

{

   echo\'they were wrong\';

}

else

{

  echo\'they guessed it right\';

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.