mysql 数据到 appcelerator-titanium mobile
mysql data to appcelerator-titanium mobile
我正在尝试找到一种方法,将数据从我的 mysql 数据库获取到我的 appcelerator 应用程序 (android)。我正在使用 PHP 脚本和 http 请求来解析 json,但我做错了什么。我知道我的数据库结构和 json 的工作原理,因为我可以使用 php 在网络上打印它,所以问题似乎出在我的 js 代码或我创建 json 的方式文件 php。我在下面的示例中尝试做的是在我的应用程序中显示 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();
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"
}
]
"What I am trying to do in my example down here is to have an alert in my app showing the "employee_name" 来自 json 中的第一行 ("Steve")。"
然后您需要引用数组中的第一个元素 (json[0].employee_name
)。请记住,计算机从 zed 开始计数,而不是 one。
我正在尝试找到一种方法,将数据从我的 mysql 数据库获取到我的 appcelerator 应用程序 (android)。我正在使用 PHP 脚本和 http 请求来解析 json,但我做错了什么。我知道我的数据库结构和 json 的工作原理,因为我可以使用 php 在网络上打印它,所以问题似乎出在我的 js 代码或我创建 json 的方式文件 php。我在下面的示例中尝试做的是在我的应用程序中显示 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();
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"
}
]
"What I am trying to do in my example down here is to have an alert in my app showing the "employee_name" 来自 json 中的第一行 ("Steve")。"
然后您需要引用数组中的第一个元素 (json[0].employee_name
)。请记住,计算机从 zed 开始计数,而不是 one。