PHP Answer class application interface Instance _php skill

Source: Internet
Author: User
Tags php programming prepare stmt

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.