arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help]Php and Mysql
#1
Hello people,

                    I directly come to the point and ask my question..I was trying to do something using php and mysql.Its just a game type thing in which a random question is picked from database using rand(min,max) and answer given by user is matched with the answer of the database.

So, table is quest containing columns qid,question,answer

PS: I tried with my stupid approach and trying to explore more ways but none of them are working.I am learning so any guidance will be appreciated..

Thanks,
Regards,
Jayant.
#2
So that means some of the questions would have same answers as that of what's user have written ?
Can you try to give an example for more clearity ?
Thanks,
 Administrator
admin@post4vps.com


#3
No, the question and answers are not repeated..Each question has an id,question and answer..I was just making a thing by which I don't have to send user an OTP or activation email,my aim is to ask a random question to a newly registered user then,If he answer the question correctly (i.e his answer matches with the answer of asked question from database),then I will update a column and mark the user as active.Hope it clears what I want to achieve,but If not,then I post a code here so that It will make you clear about my question.

Assume,something like this:
qid question answer
1 2 + 2 4
2 4 / 2 2
3 2 x 2 4

My select query:
PHP Code: (Select All)
$random_id rand(1,3);
$qry SELECT from quest_table WHERE qid $random_id;
$result mysql_query($qry,$con);//$con is a php file containing connection details
while ($row mysql_fetch_assoc($result))  
{  
echo 
$row['question];//A random question is visible in form


Suppose, question is 2 + 2,so user must answer 4(so it gets matched with the answer of the table). Waiting for reply.
#4
ohk tried something.
for example here are a bunch of questions with there answers defined at question_list table by the admin/you at the db.
[Image: 4be6c15166.png]
here is he code:
PHP Code: (Select All)
$sql "SELECT * FROM question_list order by RAND() LIMIT 1";
$result $connect_db->query($sql);
$row $result->fetch_assoc();
$question $row['question'];
$answer $row['answer'];
echo 
"<b>Question: </b>".$question."<br><b>Answer: </b>".$answer

and here is the live review what the code does:
[Image: ae1f5b2ab5.gif]


And if you want do implement a checking type system then you can try this:
index.php:
PHP Code: (Select All)
include 'config.php';
$sql "SELECT * FROM question_list order by RAND() LIMIT 1";
$result $connect_db->query($sql);
$row $result->fetch_assoc();
$question $row['question'];
$answer $row['answer'];
if(
$row != null) {
echo 
"<b>Question: </b>".$question."<br>";
echo 
"<form method='POST' action='check.php'>
<input type='hidden' name='question' value='"
.$question."'/>
<input type='text' placeholder='Type Answer' name='answer'/>
<button type='submit'>SUBMIT</button>
</form>"
;
//echo "<b>Question: </b>".$question."<br><b>Answer: </b>".$answer;

here i have used config.php for logging the php file to the system..

and the check.php file can be like this:
PHP Code: (Select All)
include 'config.php';
$question $_POST['question'];
$sql "SELECT * FROM question_list WHERE question='$question'";
$result $connect_db->query($sql);
$row $result->fetch_assoc();
$question $row['question'];
$answer $row['answer'];
if(
$_POST['answer'] == $answer) {
    echo 
'Correct!';
} else {
    echo 
'Wrong!';

and here is its live demo:
[Image: 8f6c8c59b1.gif]

if you want config.php also then here it is:
PHP Code: (Select All)
$connect_db mysqli_connect("hostname here""username of account of mysql""accountt password""databasename");
if(!
$connect_db) {
    die(
"<b>Warning!</b> Connection Failed: ".mysqli_connect_error());

 Administrator
admin@post4vps.com


#5
Thank you so much for help..Loved your explaination,it was just a few things but I made things critical,but your code made things so easier for me and helped me alot.I think I to have develop my logics and should practise more..Will post more here if I will need help. Smile
#6
Closing this topic since the Problem has been resolved.
Use report button if you wants to reopen the thread.
 Administrator
admin@post4vps.com


lockThread Closed 


Possibly Related Threads…
Thread
Author
Replies
Views
Last Post

person_pin_circle Users browsing this thread: 1 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting