Android Question read all rows in a MySql db with PHP

WebQuest

Active Member
Licensed User
Hi communities, I'm having a problem with a for loop in PHP. I need to read all the rows in a MySql table. At the moment I can only transfer one row with the Json to be able to use it in my app. I'm probably wrong with the syntax in PHP. Could you help me?
PHP:
<?php



$databasehost = "localhost";
$databasename = "xxxxxx";
$databaseusername ="xxxxxx";
$databasepassword = "xxxxxxx";




$con = mysqli_connect($databasehost,$databaseusername,$databasepassword,$databasename) or die(mysqli_error($con));

mysqli_select_db($con,$databasename) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");

mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{


  $result = mysql_query("select Post FROM Comande");
   $RowCount = mysql_fetch_all($result);     //Total size recovery

   $result2 = mysql_query("select Post FROM Comande");
    $RowCount2 = mysql_fetch_all($result2);
 
    $rows2 =array($RowCount2) ;
  for ($i = 0; $i <= $RowCount; ++$i)
  {
      $rows2[$i]
      $res = json_encode($rows2,True);
      echo $res;
      mysqli_free_result($sth);
  }



}
mysqli_close($con);

?>


B4X:
public Sub GetAllPost_Command

    ExecuteRemoteQueryGetAll("SELECT Post FROM Comande", GetAllPost)

End Sub



Sub ExecuteRemoteQueryGetAll(Query As String, JobName As String)

    Dim job As HttpJob

    job.Initialize(JobName, Me)

    job.PostString("https://www.xxxxxxxxxxxxx.com/Client/Scriptxxxxxxxxx.php", Query)

End Sub
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Try this:
PHP:
<?php



$databasehost = "localhost";
$databasename = "xxxxxx";
$databaseusername ="xxxxxx";
$databasepassword = "xxxxxxx";




$con = mysqli_connect($databasehost,$databaseusername,$databasepassword,$databasename) or die(mysqli_error($con));

mysqli_select_db($con,$databasename) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");

mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{
 
    $sth = mysqli_query("select Post FROM Comande");
    $rows = array();
    while($r = mysqli_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    print json_encode($rows);
 
}
mysqli_close($con);

?>
 
Upvote 0

WebQuest

Active Member
Licensed User
Hello thanks for the reply. i was already trying with the while loop i only get 1 item. In the case Process_Data it is fine. But for the GetAllPost case I am not getting any response from the server. I state that I am using two different scripts for the two cases. The problem that the GetAllPost case is not selected how do I select it?

B4X:
Sub JobDone(Job As HttpJob)
   
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(res)
       
        Select Job.JobName
           
            Case GetAllPost
                Dim List1 As List
                List1.Initialize
                List1 = parser.NextArray 'returns a list with maps
                For i = 0 To List1.Size - 1
                 Log(List1.Get(i))
                Next          
           
            Case Process_Data
                ParallelTime = DateTime.Time(DateTime.Now)
                Dim ListCountRow As List
                Dim ListCommand As ListCommand
                ListPostServer.Clear
                ListCountRow.Initialize
                ListCountRow.Clear
                Dim List As List
                List.Initialize
           
                List = parser.NextArray 'returns a list with maps
               
                If List.Size>0 And List.Size>NrID Then
                    ListCountNewPost=List
                   
                    For i = 0 To List.Size - 1
                        Dim m As Map
                        m=CreateMap("ID":NrID,"PostTime":PostTime,"Post":Post,"NrAntipasti":NrAnt,"NrSalat":NrSalat,"NrPizze":NrPizze,"NrPaste":NrPaste,"NrCarne":NrCarne,"NrPesce":NrPesce)
                        m=List.Get(i)
                        ListCountRow.Add(i)
                       
                        ListCommand.PostTime=m.Get("PostTime")
                        ListCommand.NrAnt=m.Get("NrAntipasti")
                        ListCommand.NrSal=m.Get("NrSalat")
                        ListCommand.NrPizze=m.Get("NrPizze")
                        ListCommand.NrPaste=m.Get("NrPaste")
                        ListCommand.NrCarne=m.Get("NrCarne")
                        ListCommand.NrPesce=m.Get("NrPesce")
                       
                        ListCommand.NrID= m.Get("ID")
                        ListCountNewPost.Add(m.Get("ID"))
                       
                        'Log(ListCommand.NrID&CRLF&ListCommand.NrAnt&CRLF&ListCommand.NrSal&CRLF&ListCommand.NrPizze&CRLF&ListCommand.NrPaste&CRLF&ListCommand.NrCarne&CRLF&ListCommand.NrCarne&CRLF&ListCommand.NrPesce)
                    Next
               
                    ListPostServer.Add(m.Get("Post"))
               
                    PostTime=ListCommand.PostTime
                    intAnt=ListCommand.NrAnt
                    Intsal=ListCommand.NrSal
                    Intpast=ListCommand.NrPaste
                    intCar=ListCommand.NrCarne
                    IntPesce=ListCommand.NrPesce
                   
                    For i=0 To ListPostServer.Size-1
                        CustomListComande.Add(CreateListItemComanda(ListPostServer.Get(i),49%y,46%x),ListPostServer.Get(i))
                    Next
                        NrID=NrID+1
                    intb=intb+1
                    BTNotifica1.Text=intb
                    BTNotifica2.Text=intb
                    Log(CRLF&"Response from server2: " & res)
                End If
               
              End Select
    Else
        Log(Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

B4X:
Sub  GetAllPost_inServer (JsonString As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(Servername & ScriptGetAllPost, JsonString)
    Wait For (j) JobDoneGetAll (j As HttpJob)
    Log(j.GetString)
    Dim Result As String = j.GetString
    j.Release
   
    Return Result
End Sub

public Sub GetAllPost_Command
    ExecuteRemoteQueryGetAll("SELECT Post FROM Comande", GetAllPost)
End Sub

Sub ExecuteRemoteQueryGetAll(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("https://www.xxxxxxxx.com/Client/Scriptxxxxxx.php", Query)
End Sub
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
If i was in this situation i would put logging everywhere
Log(JobName & " started") in executeremotequerygetall
Log(job.jobname & " Complete" in jobdone if sucessful
Presently you have a firm belief in how the logic is going to flow so add logging to ensure its happening as you expect.
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Try this:
PHP:
<?php



$databasehost = "localhost";
$databasename = "xxxxxx";
$databaseusername ="xxxxxx";
$databasepassword = "xxxxxxx";




$con = mysqli_connect($databasehost,$databaseusername,$databasepassword,$databasename) or die(mysqli_error($con));

mysqli_select_db($con,$databasename) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");

mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{

    $sth = mysqli_query("select Post FROM Comande");
    $rows = array();
    while($r = mysqli_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    print json_encode($rows);

}
mysqli_close($con);

?>
This
PHP:
while($r = mysqli_fetch_assoc($sth)) {

        $rows[] = $r;

}
should be
PHP:
while($r = mysqli_fetch_assoc($sth)) {

        array_push($rows,$r);

}
 
Upvote 0

KevinSuzanne

Member
Licensed User
Hi can any one help, how do you add say 7 columns to listview to get the data from mysql i have 7 columns that i need to get the data from msql to listview

Thankyou...
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Just reply with your table structure "columns names/types", so I can show you example with your columns names, it is simple don't worry about.
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
PHP Code:
<?php



$databasehost = "localhost";
$databasename = "xxxxxx";
$databaseusername ="xxxxxx";
$databasepassword = "xxxxxxx";




$con = mysqli_connect($databasehost,$databaseusername,$databasepassword,$databasename) or die(mysqli_error($con));

mysqli_select_db($con,$databasename) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");

mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{

    $sth = mysqli_query("SELECT Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday FROM tablename");
    $rows = array();
    while($r = mysqli_fetch_assoc($sth)) {
        array_push($rows,$r);
    }
    print json_encode($rows);

}
mysqli_close($con);

?>
change tablename to your table name.

B4A Code:
Sub populate_table()
   
    Dim j As HttpJob
    j.Initialize("j",Me)
    j.Download("http://your_php_code_url")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
       
        B4XTable1.Clear
        B4XTable1.AddColumn("Monday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Tuesday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Wednesday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Thursday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Friday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Saturday",B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Sunday ",B4XTable1.COLUMN_TYPE_TEXT)
   
        Dim Data As List
        Data.Initialize
   
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As List = parser.NextArray
        For Each colroot As Map In root
            Dim row(7) As Object
            row(0) = colroot.Get("Monday")
            row(1) = colroot.Get("Thursday")
            row(2) = colroot.Get("Friday")
            row(3) = colroot.Get("Sunday")
            row(4) = colroot.Get("Wednesday")
            row(5) = colroot.Get("Tuesday")
            row(6) = colroot.Get("Saturday")
            Data.Add(row)
        Next

        B4XTable1.SetData(Data)
       
    End If
   
   
   
End Sub
You must add B4XTable1 view from designer (before select its library)
JSON library and httputils2 library required.
 
Upvote 0

KevinSuzanne

Member
Licensed User
Getting this error

B4A Version: 10.50
Parsing code. Error
Error parsing program.
Error description: Unknown type: customlistview
Are you missing a library reference?
Error occurred on line: 33 (B4XTable)
Public clvData As CustomListV
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Add Xui views library
 
Upvote 0

KevinSuzanne

Member
Licensed User
Thankyou
mcqueccu

Error

B4A Version: 10.50
Parsing code. (0.10s)
Java Version: 11
Building folders structure. (0.03s)
Running custom action. (0.04s)
Compiling code. Error
Error compiling program.
Error description: Current declaration does not match previous one.
Previous: {Type=B4XView,Rank=0, RemoteObject=True}
Current: {Type=List,Rank=0, RemoteObject=True}
Error occurred on line: 50
Dim Root As List = parser.NextArray
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
Dim Root As List = parser.NextArray

root is used for your b4xpages declaration already
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
B4X:
Dim myRoot As List = parser.NextArray
        For Each colroot As Map In myRoot
            Dim row(7) As Object
            row(0) = colroot.Get("Monday")
            row(1) = colroot.Get("Thursday")
            row(2) = colroot.Get("Friday")
            row(3) = colroot.Get("Sunday")
            row(4) = colroot.Get("Wednesday")
            row(5) = colroot.Get("Tuesday")
            row(6) = colroot.Get("Saturday")
            Data.Add(row)
        Next
 
Upvote 0
Top