sending multidimensional data array from app to web server issues

You must Login before you can answer or comment on any questions.

Hey -

I have asked this question before and thought I was going down the right path of figuring it out and turns out that I did not. I am going to provide snippets of code I have tried the results that I have received.

My goal is to send data from my sqlite db to a webserver db

this is how i extract the db from the sqlite db:

var dataArray = []
 
var rows = db.execute('SELECT * from properties')
 
    while (rows.isValidRow())
    {
 
        dataArray.push({address:'' + rows.fieldByName('address') + '', city:'' + rows.fieldByName('city') + '', state:'' + rows.fieldByName('state') + ''});
        rows.next();    
    };
to send the data i use this
var JSONstring = JSON.stringify(dataArray);
 
var loginReq = Titanium.Network.createHTTPClient();
    loginReq.open("POST","http://localhost/arrays.php");  
 
loginReq.send(
    JSONstring
);
on the server side I do this
$json = $_POST;
 
$jsondecoded = json_decode($json);
 
$username = $json;  
 
$tableselectall = "SELECT * FROM users";
$rownum = mysql_num_rows(mysql_query($tableselectall)); 
 
$sql2 = "INSERT INTO users (id, username, password, name, email) VALUES('$rownum'+1, '$username', 'test3', 'test3', 'test3@test.com')";
 
mysql_query($sql2);
My results in my phpMySql db for the username is:

'Array'

if i do this in php

$username = $json[0];
My results in my phpMySql db for the username is:

' ' (a blank space)

if do this in php

$username = $jsondecoded
My results in my phpMySql db for the username is:

' ' (a blank space)

if do this in php

$username = $jsondecoded[0]
no new entry is placed in my db

for reference 'Titanium.API.log(JSONstring)' outputs

[{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lka","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"LK"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Something","city":"Toronto","state":"Ca"},{"address":"Something","city":"Toronto","state":"Ca"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"124","city":"Lk","state":"Lk"},{"address":"124","city":"Lk","state":"Lk"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"5606 ","city":"Stone mountain","state":"Ga"}]
My goal is to be able to just pull the first entry for address...

2 Answers

You're sending the data in such a way that its not trying to send a string, its actually trying to set multiple variables. Change your send to:

loginReq.send({
    data:JSONstring
});
Then in your php, read in your post data as:
$json = $_POST['data'];
After that, you should be printing out your data to the error log way before you get the database. Just to ensure that you have the right data at each step. So print out json and jsondecoded to the error log so that you can see the data is coming in correctly. After that, you should be able to access your data and insert it into the database.

— answered 8 months ago by Anthony Decena
answer permalink
3 Comments
  • So i have tried that as well and get the same issues i will post later...BUT can you please tell me how to post to the error log??? I know how to view the data in the app that is being sent by doing Titanium.API.log() but I have no idea how to view the data once it is sent to the server and executes the php script....where would that data be logged to because the script is on the server, is it sent back to the app???

    — commented 8 months ago by tareq dowla

  • When I do

    loginReq.send({
        data: JSONstring
    });
    and in php have
    $json = $_POST['data'];
     
    print_r($json);
     
    echo "test";
     
    $jsondecoded = json_decode($json);
     
    $username = $json;
    in the db I get

    [{"address":"Adf","city":"Lk","state":"Lk"},{"addr...

    if I do

    $username = $jsondecoded;
    in the db i get

    'Array'

    if i do

    $username = $jsondecoded[0][0];
    in the db i get

    no entry

    if i do

    $username = $jsondecoded['address'][0];
    in the db i get

    ' ' (blank space)

    in the Titanium.API.log({data: JSONstring});

    i get

    data = "[{\"address\":\"Adf\",\"city\":\"Lk\",\"state\":\"Lk\"},{\"address\":\"Adf\",\"city\":\"Lk\",\"state\":\"Lk\"},{\"address\":\"Adf\",\"city\":\"Lk\",.....

    — commented 8 months ago by tareq dowla

  • How you print to your servers error log is dependent on your server. In general there is a PHP command error_log(msg) that will print msg to the error log. You can then see that print out in real time by just tailing your log file. Regardless, it seems that the data is getting to the server and the issue is on the server side. You question may be better answered on the PHP forum.

    — commented 8 months ago by Anthony Decena

There is no need to stringify object before sending in line

var JSONstring = JSON.stringify(dataArray);

The HTTPClient's send() method automatically stringifies data for you.So try like this:

var JSONstring = dataArray; and now send it.

— answered 8 months ago by Shalini Kushwaha
answer permalink
1 Comment
  • so when I do that i still get the same problems, i can get the array to be in there but i get blanks whenever i try to access elements from it....maybe im doing the access of the array incorrectly?

    $json = $_POST['data'];
     
    $jsondecoded = json_decode($json);
     
    $username = $jsondecoded[0]; $username = $jsondecoded['address'];   $username = $jsondecoded['address'][0]

    — commented 8 months ago by tareq dowla

Your Answer

Think you can help? Login to answer this question!