This article describes the application of PHP answer class interface implementation methods. Share to everyone for your reference. The implementation methods are as follows:
question_get.php files are as follows:
Copy Code code as follows:
<?php
Session_cache_expire (60);
Session_Start ();
if (!isset ($_session[' zaszh_user_id ')) {
echo json_encode (' status ' => ' Error ', ' msg ' => ' connection timed out, please reopen the page.) '));
Exit
}
$user _id = $_session[' zaszh_user_id '];
$user _id = 1; Test with
Random access to 5-way questions
$question _id = Array ();
while (sizeof ($question _id) <5) {
$num _rand = Mt_rand (1,114);
if (!in_array ($num _rand, $question _id))
$question _id[] = $num _rand;
}
Require (' connect_database.php ');
Number of questions
$mysqli->query ("Update zaszh_user set answer_surplus=answer_surplus-1 where id={$user _id} and Answer_surplus>0" );
if ($mysqli->affected_rows) {
There are remaining times
}else{
No remaining number
echo json_encode (' status ' => ' Error ', ' msg ' => ' The remaining number of answers today has been used up, come again tomorrow Oh ~ '));
$mysqli->close ();
Exit
}
Topic
if ($stmt = $mysqli->prepare ("Select Question,a,b,c,d,answer from zaszh_question where ID in (?,?,?,?,?)") {
$stmt->bind_param (' iiiii ', $question _id[0], $question _id[1], $question _id[2], $question _id[3], $question _id[4]);
$stmt->execute ();
$stmt->bind_result ($question, $A, $B, $C, $D, $answer);
$rows = Array ();
while ($stmt->fetch ()) {
$rows [] = Array (
' Question ' => $question,
' A ' => $A,
' B ' => $B,
' C ' => $C,
' D ' => $D,
' Answer ' => $answer
);
}
Answer Records
if ($stmt = $mysqli->prepare ("INSERT INTO Zaszh_answer (user_id,question1,question2,question3,question4,question5 , create_date) VALUES (?,?,?,?,?,?, Unix_timestamp (now))) {
$stmt->bind_param (' iiiiii ', $user _id, $question _id[0], $question _id[1], $question _id[2], $question _id[3],$ QUESTION_ID[4]);
$stmt->execute ();
if ($answer _id = $stmt->insert_id) {
$param = Array (
' answer_id ' => $answer _id
);
Echo Json_encode (Array_merge ($rows, $param));
}else{
echo json_encode (' status ' => ' Error ', ' msg ' => ') system error. '));
}
}
$stmt->close ();
}
$mysqli->close ();
I hope this article will help you with your PHP programming.