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"
}
]