PHP Answer class application interface instance, PHP answer interface instance
This paper describes the implementation of PHP answer class application interface. Share to everyone for your reference. The implementation method is as follows:
The question_get.php file is as follows:
Copy CodeThe code is as follows: <?php
Session_cache_expire (60);
Session_Start ();
if (!isset ($_session[' zaszh_user_id ')) {
echo json_encode (' status ' = ' error ', ' msg ' = ' + ' connection timeout, please reopen the page. '));
Exit
}
$user _id = $_session[' zaszh_user_id ');
$user _id = 1; For testing
Randomly get 5 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 answers
$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 times left
}else{
No number of times left
echo json_encode (' status ' = ' error ', ' msg ' = ' = ' The remainder of today's answer has been exhausted, come back 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 record
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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/955398.html www.bkjia.com true http://www.bkjia.com/PHPjc/955398.html techarticle PHP Answer class application Interface example, PHP answer interface Example This article describes the PHP answer class application interface implementation method. Share to everyone for your reference. The specific implementation method is as follows: Q ...