无法显示大量JSON数据

时间:2017-10-19 02:06:13

标签: php mysql json phpmyadmin

我们正在使用将在线部署其数据库的Android应用程序。我们正在使用托管网站提供的phpmyadmin。 我们通过使用JSON和PHP从该数据库中获取数据。

一开始没有问题,因为数据库表中的数据量很少。当php需要显示大量数据时会出现问题。

看看这个api电话:

http://pogo.x10host.com/cgi-bin/database/v1/Api.php?apicall=getlandmarks&region_id=3

并将其与此进行比较:

http://pogo.x10host.com/cgi-bin/database/v1/Api.php?apicall=getlandmarks&region_id=1

修改

DbConnect.php:

<?php 
header('charset = utf8')
//Class DbConnect
class DbConnect
{
    //Variable to store database link
    private $con;

    //Class constructor
    function __construct()
    {

    }

    //This method will connect to the database
    function connect()
    {
        //Including the constants.php file to get the database constants
        include_once dirname(__FILE__) . '/Constants.php';

        //connecting to mysql database
        $this->con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

        //Checking if any error occured while connecting
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        //finally returning the connection link 
        return $this->con;
    }
}
?>

Api.php(用于显示):

<?php 
header('CONTENT-TYPE: application/json');

//getting the dboperation class
require_once '../includes/DbOperation.php';

//function validating all the paramters are available
//we will pass the required parameters to this function 
function isTheseParametersAvailable($params){
    //assuming all parameters are available 
    $available = true; 
    $missingparams = ""; 

    foreach($params as $param){
        if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
            $available = false; 
            $missingparams = $missingparams . ", " . $param; 
        }
    }

    //if parameters are missing 
    if(!$available){
        $response = array(); 
        $response['error'] = true; 
        $response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';

        //displaying error
        echo json_encode($response);

        //stopping further execution
        die();
    }
}

//an array to display response
$response = array();

//if it is an api call 
//that means a get parameter named api call is set in the URL 
//and with this parameter we are concluding that it is an api call
if(isset($_GET['apicall'])){

    switch($_GET['apicall']){

        case 'getregions':
            $db = new DbOperation();
            $response['error'] = false; 
            $response['message'] = 'REGIONS';
            $response['regions'] = $db->getRegions();
        break; 

        case 'getlandmarks':
            if(isset($_GET['region_id'])){
                $db = new DbOperation();
                if($db->getLandmarks($_GET['region_id'])){
                    $response['error'] = false; 
                    $response['message'] = 'LANDMARKS';
                    $response['landmarks'] = $db->getLandmarks($_GET['region_id']);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'Some error occurred please try again';
                }
            }else{
                $response['error'] = true; 
                $response['message'] = 'Nothing to delete, provide an id please';
            }
        break; 

        case 'getfestivals':
            if(isset($_GET['month'])){
                $db = new DbOperation();
                if($db->getFestivals($_GET['month'])){
                    $response['error'] = false; 
                    $response['message'] = 'FESTIVALS';
                    $response['festivals'] = $db->getFestivals($_GET['month']);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'Some error occurred please try again';
                }
            }else{
                $response['error'] = true; 
                $response['message'] = 'Nothing to delete, provide an id please';
            }
        break; 

        case 'getallfestivals':
            $db = new DbOperation();
            $response['error'] = false; 
            $response['message'] = 'ALL FESTIVALS';
            $response['festivals'] = $db->getAllFestivals();
        break; 

        case 'getrestaurants':
            if(isset($_GET['region_id'])){
                $db = new DbOperation();
                if($db->getRestaurants($_GET['region_id'])){
                    $response['error'] = false; 
                    $response['message'] = 'RESTAURANTS';
                    $response['restaurants'] = $db->getRestaurants($_GET['region_id']);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'Some error occurred please try again';
                }
            }else{
                $response['error'] = true; 
                $response['message'] = 'Nothing to delete, provide an id please';
            }
        break; 

        case 'gethotels':
            if(isset($_GET['region_id'])){
                $db = new DbOperation();
                if($db->getHotels($_GET['region_id'])){
                    $response['error'] = false; 
                    $response['message'] = 'HOTELS';
                    $response['hotels'] = $db->getHotels($_GET['region_id']);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'Some error occurred please try again';
                }
            }else{
                $response['error'] = true; 
                $response['message'] = 'Nothing to delete, provide an id please';
            }
        break; 


        case 'gettrivias':
            $db = new DbOperation();
            $response['error'] = false; 
            $response['message'] = 'TRIVIAS';
            $response['trivia'] = $db->getTrivias();
        break; 

        case 'getfeatured':
            $db = new DbOperation();
            $response['error'] = false; 
            $response['message'] = 'DESTINATION';
            $response['landmarks'] = $db->getFeaturedDestination();
        break; 

        /* case 'getjobs':
            if(isset($_GET['region_id'])){
                $db = new DbOperation();
                if($db->getJobs($_GET['region_id'])){
                    $response['error'] = false; 
                    $response['message'] = 'JOBS';
                    $response['jobs'] = $db->getJobs($_GET['region_id']);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'Some error occurred please try again';
                }
            }else{
                $response['error'] = true; 
                $response['message'] = 'Nothing to delete, provide an id please';
            }
        break;  */
    }

}else{
    //if it is not api call 
    //pushing appropriate values to response array 
    $response['error'] = true; 
    $response['message'] = 'Invalid API Call';
}

//displaying the response in json structure 
echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:0)

删除你的

$this->con->set_charset("utf8");

然后在您的函数中添加function connect() { //Including the constants.php file to get the database constants include_once dirname(__FILE__) . '/Constants.php'; //connecting to mysql database $this->con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); $this->con->set_charset("utf8"); //Checking if any error occured while connecting if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //finally returning the connection link return $this->con; }

MFCC