【问题标题】:mysql data to appcelerator-titanium mobilemysql数据到appcelerator-titanium mobile
【发布时间】:2016-08-23 14:12:39
【问题描述】:

我正在尝试找到一种方法来从我的 mysql 数据库中获取数据到我的 appcelerator 应用程序 (android)。我正在使用 PHP 脚本和 http 请求来解析 json,但我做错了。我知道我的数据库结构和 json 的工作原理,因为我可以使用 php 在 web 上打印它,所以问题似乎出在我的 js 代码或我使用 php 创建 json 文件的方式上。我在下面的示例中尝试做的是在我的应用程序中显示一个警报,显示 json 中第一行(“Steve”)中的“employee_name”。

有人可以帮我理解我做错了什么吗?

我一直在研究这个例子 https://archive.appcelerator.com/question/51201/how-to-create-mysql-query-from-titanium-mobile 和其他人,但没有成功。

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

测试.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"
    }
]

【问题讨论】:

    标签: php json appcelerator


    【解决方案1】:

    “我在下面的示例中尝试做的是在我的应用程序中显示一个警报,显示 json 中第一行(“Steve”)中的“employee_name”。”

    然后你需要引用数组中的第一个元素(json[0].employee_name)。请记住,计算机从 zed 开始计数,而不是一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2016-04-04
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多