Android Question SOLVED - Problem Updating mySQL db with array / JSON string via PHP .

mangojack

Well-Known Member
Licensed User
Longtime User
In the process of learning MySQl, PHP etc. I am attempting to export data from a local SQL db to mySQL via PHP script following examples in this thread by @KMatle Httputils2 send large arrays as JSON string via job download2 and PHP

I cannot send the entire (small) SQL table in one hit. I can send certain amount of records but there appears to be a maximum limit .. 6 Records.
Have run tests and scoured the forum to no avail.

Is there something I'm Missing .. JSON string size (doubted that .. but now reading threads raising this as possibility ...) , MySQL / Host settings etc

Many Thanks ...

B4X:
Sub btnSendBrowse_Click

   Dim JSONList As List
   JSONList.Initialize

   'get all records and populate map ... TABLE Browse_Links (ID INTEGER PRIMARY KEY, Tag TEXT , Title TEXT, url TEXT)")
   'There is 8 Records in this Table

   cursor1 = SQL1.ExecQuery("SELECT * FROM  Browse_Links")

   Dim numRows As Int = cursor1.RowCount  
   Log (numRows)     '### logs 8 records

   'For  i = 0 To numRows -1       '###  FAIL  mySQL db NOT updated !

   'For i = 0 To 5      '##  OK 6 records inserted
   'For i = 1 To 6     '##    OK 6 records inserted
   'For i = 2 To 7     '##  FAIL  Nil records inserted  ##
   For i = 3 To 7     '##    OK 5 records inserted

     Dim mapData As Map
     mapData.Initialize

     cursor1.Position = i
     mapData.put("ID", cursor1.GetInt("ID"))
     mapData.put("Tag",cursor1.GetString("Tag") )
     mapData.put("Title", cursor1.GetString("Title"))
     mapData.put("Url",cursor1.GetString("url"))

     JSONList.add(mapData)

     '###  all records listed correctly
     lvwData.AddSingleLine(cursor1.GetInt("ID") & "  " & cursor1.GetString("Tag") & "  " & cursor1.GetString("Title") & "  " & cursor1.GetString("url"))
   Next

   cursor1.Close

   Dim JSONGen1 As JSONGenerator
     JSONGen1.Initialize2(JSONList)

   Dim JSONstring As String
     JSONstring = JSONGen1.ToString

   Log (JSONstring)  '### log confirms all 8 records included

   Dim InsertJob As HttpJob
  InsertJob.Initialize("InsertBrowse", Me)
  InsertJob.download2("http://myhost.net/scripts/recipes/insert_script.php", Array As String ("Action", "InsertBrowse", "MyJSON", JSONstring))

End Sub

PHP ..
B4X:
case "InsertBrowse":

   $json = $_GET["MyJSON"];
  $jsall = array();
  $jsone = array();
  $jsall=json_decode($json, true);
  $x = 0;
  while($x < count($jsall)) {
      $jsone=$jsall[$x];

      $id=$jsone["ID"];
      $tag=$jsone["Tag"];
      $title=$jsone["Title"];
      $url=$jsone["Url"];

  
       $q = mysql_query("INSERT INTO browse_tbl (table_id, tag, title, url) VALUES ($id, '$tag', '$title', '$url')");
  
  $x++;
  }
     print json_encode("Inserted Records");
   break;
 
Last edited:

mangojack

Well-Known Member
Licensed User
Longtime User
the lenght of GET depends on the browser so there is not valid size limit. on IE it can be 2048 chars and on firefox 1024 just to give an example.

not sure what it on on android.
This is all new stuff to me .. to a layman are you saying the code in post#1 has possably failed because the JSon string sent to the PHP script was too large. ?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
right, it gets cut off due to the size limitations of the GET send and sometimes you get server side errors that it can't deal with that url.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
the lenght of GET depends on the browser so there is not valid size limit. on IE it can be 2048 chars and on firefox 1024 just to give an example.

not sure what it on on android.
just re reading posts :confused: ... is it a suggestion to change GET to POST / LET / SET ????

I'm Still trying to understand GET and POST statements ... to much to quick .. thankyou

Cheers
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Luca was just joking around.

do a replace of $_GET to $_POST in the PHP

in B4A you need to look for the POST variant of your http call
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
dljob.PostString("http://url/page.php",jsondata)
Ok ... will work on that ..
But if thats the case I then have to come up with another method to pass "action" type .. and I was led to believe 'Download2' had its benefits similar to 'ExecQuery2' in that you do not have to escape strings / parameter sytax etc propery.

B4X:
InsertJob.download2("http://myhost.net/scripts/recipes/insert_script.php", Array As String ("Action", "InsertBrowse", "MyJSON", JSONstring))

Thanks for you time ..
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
not really you can use this (example was not 100% right but was just to give you the idea)

B4X:
dljob.PostString("http://url/page.php","action="&action&"&data="&jsondata)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
adapted to your situation

B4X:
InsertJob.PostString("http://myhost.net/scripts/recipes/insert_script.php","action=InsertBrowse&MyJSON="&JSONstring)
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
adapted to your situation

Thanks for the last ... I was struggling with proper syntax. All records exported correctly. :)

Next Lesson ....
 
Upvote 0
Top