doujia7779 2016-08-23 14:12
浏览 68
已采纳

mysql数据到appcelerator-titanium mobile

I´m trying to find a way to get data to my appcelerator app (android) from my mysql database. I am using a PHP script and a http request to parse a json, but I am doing something wrong. I know my database structure and the json works because I can print it on the web using php, so the problem seems to be in my js code or the way I am creating a json file with php. What I am trying to do in my example down here is to have an alert in my app showing the "employee_name" from the first row ("Steve") in the json.

Can someone please help me to understand what I´m doing wrong?

I been looking into this example https://archive.appcelerator.com/question/51201/how-to-create-mysql-query-from-titanium-mobile and others, but without success.

App.js

var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(){
var json = JSON.parse(this.responseText);
if (!json) { 
    Titanium.API.info('Error - Null return!'); 
    return;
}
alert(json[1].employee_name);
};
xhr.open('GET', "http://ljudy.com/test.php");
xhr.send();

test.PHP

<?php

$connection =    mysqli_connect("localhost","ljudycom_andreas","****","ljudycom_test") or die("Error " . mysqli_error($connection));


$sql = "select * from tbl_employee";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
$fp = fopen('empdata.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);

//close the db connection
mysqli_close($connection);
?>

JSON

[
    {
        "employee_id" : "1",
        "employee_name" : "Steve",
        "designation" : "VP",
        "hired_date" : "2013-08-01",
        "salary" : "60000"
    }, {
        "employee_id" : "2",
        "employee_name" : "Robert",
        "designation" : "Executive",
        "hired_date" : "2014-10-09",
        "salary" : "20000"
    }, {
        "employee_id" : "3",
        "employee_name" : "Luci",
        "designation" : "Manager",
        "hired_date" : "2013-08-20",
        "salary" : "40000"
    }, {
        "employee_id" : "4",
        "employee_name" : "Joe",
        "designation" : "Executive",
        "hired_date" : "2013-06-01",
        "salary" : "25000"
    }, {
        "employee_id" : "5",
        "employee_name" : "Julia",
        "designation" : "Trainee",
        "hired_date" : "2014-10-01",
        "salary" : "10000"
    }
]
  • 写回答

1条回答 默认 最新

  • duanqian8867 2016-08-23 17:12
    关注

    "What I am trying to do in my example down here is to have an alert in my app showing the "employee_name" from the first row ("Steve") in the json."

    Then you need to reference the first element in the array (json[0].employee_name). Remember that computers start counting at zed, not one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料