Не удается отобразить большое количество данных JSON
Мы работаем с приложением для Android, в котором база данных будет развернута онлайн. Мы используем phpmyadmin
предоставляется хостингом сайта. Мы извлекаем данные из этой базы данных с использованием JSON и PHP.
Сначала проблем нет, потому что в таблицах базы данных содержится мало данных. Проблема возникает, когда php нужно отображать большое количество данных.
Посмотрите на этот вызов API:
http://pogo.x10host.com/cgi-bin/database/v1/Api.php?apicall=getlandmarks®ion_id=3
и сравните это с этим:
http://pogo.x10host.com/cgi-bin/database/v1/Api.php?apicall=getlandmarks®ion_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 ответ
Решение
Удали свой
header('charset = utf8')
а затем в вашей функции добавить $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;
}