Php q & A application interface instance and php Q & A interface instance _ PHP Tutorial

Source: Internet
Author: User
Php q & A application interface instance and php Q & A interface instance. Php q & A application interface instance. php Q & A interface instance this article describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows: q PHP q & A application interface instance and php q & A interface instance

This example describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows:

The question_get.php file is as follows:

The code is as follows:

<? Php
Session_cache_expire (60 );
Session_start ();
If (! Isset ($ _ SESSION ['zaszh _ user_id ']) {
Echo json_encode (array ('status' => 'error', 'MSG '=>' connection timeout. open the page again. '));
Exit;
}
$ User_id = $ _ SESSION ['zaszh _ user_id '];

// $ User_id = 1; // used for testing

// Obtain 5 questions randomly
$ 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 ){
// Remaining times
} Else {
// No remaining times
Echo json_encode (array ('status' => 'error', 'MSG '=>' the remaining answers are used up. come back tomorrow ~ '));
$ Mysqli-> close ();
Exit;
}
// Question
If ($ stmt = $ mysqli-> prepare ("select question, A, B, C, D, answer from zaszh_question where id in (?,?,?,?,?) ")){
$ Stmt-> bind_param ('iiiiiii ', $ 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' => $,
'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 (array ('status' => 'error', 'MSG '=>' system error. '));
}
}
$ Stmt-> close ();
}
$ Mysqli-> close ();

I hope this article will help you with php programming.

Examples in this article describes how to implement the php q & A application interface. Share it with you for your reference. The specific implementation method is as follows: q...

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.