Api的空回归

时间:2018-04-04 10:47:08

标签: php mysql

我是初级Android开发人员,我被要求创建一个连接到mysql数据库并下载一些数据的应用程序。

我对PHP和mysql都不了解,所以请提前原谅我任何编写错误的代码,如果你想发表评论,从而帮助我改进我的代码编写,我我将不胜感激:)

我从数据库下载所有数据并通过我的简单API将其传递给应用程序时没有任何问题,但是当我试图仅传递另一个数据库的某些条目时(也就是说,使用带有get函数的get函数)其中的参数)我有一些问题。

我正在这里发布代码。

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; 
}

从现在开始,我正在做的是:首先(getUtenteId)我从用户的邮件中获取 submission_id 。然后来自submission_id的(getUtenteDati)我可以获得所有用户的并返回它们。我将在Api.php文件中调用两个函数。 (这里我们还在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;
 }
 }

 ?>

这是 Api.php 文件

<?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);

如果我正确地调用此API(http://.../Api/v1/Api.php?apicall=getutente&emailutente=user@mail.it),我得到的是一个空白页。

如果我在网址中写了“emailutente”错误(如emailsuteente),我没有收到错误,但回复是空的。

 {"error":false,"message":"Dati utente scaricati","messagedata":"Dati utente scaricati","utente":[]}

我尝试在getUtenteDati函数中使用echo,它会正确打印我需要的所有数据。

任何线索?

提前谢谢!

编辑:我尝试在我的本地数据库中使用此代码,它正在运行。问题仍然存在于我的远程数据库中。这很奇怪,因为在get函数中有一个简单的ECHO,我得到了我想要的所有值。但它似乎只是拒绝在阵列中将它们交给我。

1 个答案:

答案 0 :(得分:0)

只是一个小小的更新。代码实际上是正确的。问题是Wordpress数据库拒绝使用我的远程查询返回合理数据。我设法用一个简单的自制插件以更安全的方式获取它们:

global $wpdb;
[...]

if( is_user_logged_in() AND ! is_admin() ) {
    $current_user = wp_get_current_user();
    $email = $current_user->user_email;

$userid= $wpdb->get_var($wpdb->prepare("SELECT [...], $email);