简体   繁体   English

从php中的SQL数据库到Java的多个值作为单独的变量

[英]Multiple values from a SQL Database in php through to Java as separate variables

I'm attempting to retrieve multiple values from a database through php to my android java in eclipse. 我正在尝试通过php从数据库检索多个值到eclipse中的android java。 I'm managing to get all variations of json arrays except the one i need. 我正在设法获取json数组的所有变体,但我需要的除外。

My database is: 我的数据库是:

**Aircraft**        **Status**
  A870_870              1
  A870_871              1
  A870_872              1
  A870_873              1 
  A870_874              1 
  A870_875              1
  A870_876              2
  A870_877              1
  A870_878              2
  A870_879              2 
  A870_880              2
  A870_881              0
  A870_882              0
  A870_883              0
  A870_884              0
  A870_885              0

The format I need so that my android app reads it is: 我需要让我的android应用读取的格式是:

    {"A70_870":"1","A70_871":"1","A70_872":"1","A70_873":"1","A70_874":"1",
"A70_875":"1","A70_876":"2","A70_877":"1","A70_879":"2","A70_878":"2",
"A70_880":"2","A70_881":"0","A70_882":"0","A70_883":"0","A70_884":"0",
"A70_885":"0"}

I've been attempting different 'while' loops all sorts of other variations and manage to get all sorts of combinations except the one i need. 我一直在尝试不同的“ while”循环各种其他变体,并设法获得除我需要的以外的各种组合。 Surely there is a way...? 当然有办法...?

My closest PHP attemp is below: 我最接近的PHP尝试如下:

<?php
$con = mysqli_connect("localhost","root","", "mytestdatabase"); 

 if (mysqli_connect_errno()) { 
     echo 'Database connection error: ' . mysqli_connect_error(); 
     exit(); 
     } 

$userdetails = mysqli_query($con, "SELECT *FROM aircraft_status"); 

$row = mysqli_fetch_row($userdetails) ;

$result_data = array(

    'A70_870'=>$row[1],
    'A70_871'=>$row[1],
    'A70_872'=>$row[1],
    'A70_873'=>$row[1],
    'A70_874'=>$row[1],
    'A70_875'=>$row[1],
    'A70_876'=>$row[1],
    'A70_877'=>$row[1],
    'A70_879'=>$row[1],
    'A70_878'=>$row[1],
    'A70_880'=>$row[1],
    'A70_881'=>$row[1],
    'A70_882'=>$row[1],
    'A70_883'=>$row[1],
    'A70_884'=>$row[1],
    'A70_885'=>$row[1],);

echo json_encode($result_data);
?>

It gives the correct format, but obviously only reads row 1. I can't access 3,5,7 etc.. 它给出了正确的格式,但显然只能读取第1行。我无法访问3、5、7等。

If anyone can assist me in this it would be great!! 如果有人可以帮助我,那就太好了!! :) I'm sure its something simple I'm not doing right.... :)我确定这很简单,我做得不好。

I might be missing something but is it not just wrapping result extraction in a while loop: 我可能会丢失一些东西,但它不只是将结果提取包装在while循环中:

$userdetails = mysqli_query($con, "SELECT *FROM aircraft_status"); 

$result_data = array();

if ($userdetails) {
  while($row = mysql_fetch_array($userdetails)) {
    // $row[0] being aircraft and $row[1] being status
    array_push($result_data , $row[0], $row[1]);
  }

}
else {
  echo mysql_error();
}

echo json_encode($result_data);

My PHP is very rusty, so might need adjustments. 我的PHP非常生锈,因此可能需要调整。 Hope this gets you on the way. 希望这能帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM