Small Ant Learning App Interface Development (6)--app Interface Instance--the case realization of the home data interface

Source: Internet
Author: User
Tags exit in learn php

Three ways to read data

    1. Read database mode development Home interface

      Get data from database--"package--" Generate interface number

Advantages: The system with high data timeliness

2. Read cache mode Development Home interface (very wide application)

Get information from database--"package--" return data

Request again--"cache--" return data

Pros: Reduce database pressure

3. Timed Read cache mode development interface

Database--"crontab (timed Generation)

Request--"cache--" return data

Development app can install tools: Start bluestacks Android Simulator

Read the database way out of the code implementation of the interface:

<?php/************************************* read the database How to develop the homepage interface * Learn php small ant * Blog  http://my.oschina.net/ woshixiaomayi/blog*************************************///loading the DB class//code content written the day before yesterday please refer to  http://my.oschina.net/ Woshixiaomayi/blog/518295require_once ('./db.php ');//Load the previously written interface response class//code contents please refer to  http://my.oschina.net/ Woshixiaomayi/blog/517384require_once ('./response.php ');//Receive paging Data  page  for current page   pagesize   How much data $page=isset ($_get[' page ')? $_get[' page ']:1; $pagesize =isset ($_get[' pagesize '])? $_get[' pagesize ']:5 ;//Detect if these two values are numeric if (!is_numeric ($page)  or !is_numeric ($pagesize)) {//Are not numbers, send error message/* The reason for the use of a return is to enhance the readability of the program, because programmers unfamiliar with the program, do not know that there is exit in the Show method, plus a return, the other people will know that after the execution of this step, the program will stop, the subsequent program will not be executed. Convenient for other people, everyone good is really good \ (^o^)/yes!*/return response::show (400, ' argument not valid ');} Set the offset required for paging $offset= ($page-1) * $pagesize;//write SQL statement $sql= "select * from actor limit ". $ Offset. ",". $pagesize;//If there is an error connecting to the database, obtain the information and return the customized information//To avoid exposing the error directly to the user try{$connect  =db::getinstance ()->conNect ();} catch (exception  $ea) {//error message returned to App Return response::show (' Mysql not connect ');} The database connection succeeds, executes the SQL statement, gets the result set $result=mysql_query ($sql, $connect); $index _data=array (); while ($row =mysql_fetch_assoc ($ Result) {$index _data[]= $row;} if ($index _data) {//required result data to get, return data return response::show (200, ' content obtained successfully ', $index _data);} else{//not get, return error prompt Return response::show (400, ' Home data acquisition failed '); >

Note Two points to note:

    1. Add a return to make it easier for other people to read the code.

    2. When the database is not connected successfully, an exception is obtained and a prompt message is returned.

Small Ant Learning App Interface Development (6)--app Interface Instance--the case realization of the home data interface

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.