I'm a junior android developer, and i was asked to make an app that connects to a mysql database and downloads some data.
I have no knowledge at all of PHP and mysql, so please forgive me in advance for any badly written code, if you'd like to comment and thus help me improve my code writing, i'll be grateful :)
I had no issues with downloading all data from a database and passing it to the app through my simple API, but when i'm trying to pass only some entries of another database (that's to say, with a get function with a parameter in it) i'm having some issues.
I'm posting the code right here.
DbOperation.php
class DbOperation
{
private $con;
//Class constructor
function __construct()
{
require_once dirname(__FILE__) . '/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
//This is the WORKING function, i have no issues with it.
function getEvento(){
$stmt = $this->con->prepare("SELECT event_id, event_title, event_begin, event_end, event_category, event_link, event_label FROM wp_my_calendar");
$stmt->execute();
$stmt->bind_result($id, $titolo, $inizio, $fine, $categoria, $link, $luogo);
$eventi = array();
while($stmt->fetch()){
$evento = array();
$evento['id'] = $id;
$evento['titolo'] = $titolo;
$evento['inizio'] = $inizio;
$evento['fine'] = $fine;
$evento['categoria'] = $categoria;
$evento['link'] = $link;
$evento['luogo'] = $luogo;
array_push($eventi, $evento);
}
return $eventi;
}
From now on, what i'm doing is: first (getUtenteId) i get the submission_id from the mail of the user. Then (getUtenteDati) from the submission_id i can get all the user's values and return them. I'll call both get functions in the Api.php file. (Here we're still in DbOperation.php)
function getUtenteId($email){
$idutente = $this->con->prepare("SELECT submission_id FROM wp_rm_submissions WHERE user_email='$email'");
$idutente->execute();
$idutente->bind_result($sub_id);
$idutente->fetch();
//echo $sub_id;
return $sub_id;
}
function getUtenteDati($utenteid) {
$stmt = $this->con->prepare("SELECT field_id, value FROM wp_rm_submission_fields WHERE submission_id='$utenteid' ORDER BY field_id");
$stmt->execute();
$stmt->bind_result($fieldid, $valori);
$dati = array();
while($stmt->fetch()){
$dato = array();
$dato['id'] = $fieldid;
$dato['valore'] = $valori;
//echo $valori;
array_push($dati, $dato);
}
return $dati;
}
}
?>
And here's the Api.php file
<?php
require_once '../includes/DbOperation.php';
function isTheseParametersAvailable($params){
$available = true;
$missingparams = "";
foreach($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . ", " . $param;
}
}
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
echo json_encode($response);
die();
}
}
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
case 'geteventi':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Calendario aggiornato';
$response['messagedata'] = 'Data aggiornata';
$response['eventi'] = $db->getEvento();
break;
case 'getutente':
$db = new DbOperation();
//isTheseParametersAvailable(array('emailutente')); Commented because it returns error, don't know why.
$response['error'] = false;
$response['message'] = 'Dati utente scaricati';
$response['messagedata'] = 'Dati utente scaricati';
$emailutente = $_GET['emailutente'];
$idutente = $db->getUtenteId($emailutente);
//echo "id utente $idutente";
$dbdue = new DbOperation();
$response['utente'] = $dbdue->getUtenteDati($idutente);
break;
}
}else{
$response['error'] = true;
$response['message'] = 'Errore nel tentativo di aggiornare i dati, controlla la connessione a internet e riprova';
}
echo json_encode($response);
If i call this API correctly (http://.../Api/v1/Api.php?apicall=getutente&emailutente=user@mail.it), what i get is a blank page.
If i write "emailutente" wrong in the url (like emailsuteente), i get no error, but the response is empty.
{"error":false,"message":"Dati utente scaricati","messagedata":"Dati utente scaricati","utente":[]}
I tried to use an echo inside my getUtenteDati function, and it prints all the data i need correctly.
Any clues?
Thank you in advance!
EDIT: I tried to use this code in my local DB, and it's working. Issue still remains inside my remote DB. And it's weird, because with a simple ECHO inside the get function, i'm getting all the values i want. But it simply seems to refuse to give them to me inside an array.