Search code examples
phpmysqlcsvapp-inventor

PHP export MySQL to csv for app inventor


When I try to export MySQL table to csv (use php) and make it a list in app inventor, but the list doesn't show.

My Block : enter image description here

My php file :

if ($result = mysqli_query($dbc, "SHOW COLOMNS FROM Absent")){

$numberOfRows = mysqli_num_rows($result);
}
if($numberOfRows > 0) {
$values = mysqli_query($dbc, "SELECT full_name FROM Absent");
while($rowr = mysqli_fetch_row($values)){
for($j=0; $j<$numberOfRows; $j++) {
$csv_output .=$rowr [$j]. " ,";
}
$csv_output .= "\n";
}
}
print $csv_output;
exit;

Solution

  • i don't know much about app-inventor, but as for the sql queries:

    • the first table is "CUSTOMER", your second is: "Absent", is it a typo? (your'e iterating your "Absent" row with the number of columns you're getting from "CUSTOMER", was that your intention?)

    • your "$rowr" var has only 1 column ("full_name"), so you could easily: drop the "for loop", to:

      $csv_output .=$rowr [0];

    P.S. an alternative way of coding that in php would be:

    $result = mysqli_query($dbc, "SELECT column1, column2, column3 FROM Absent");
    
    while ($row = mysqli_fetch_assoc($result))
    {
       $comma = "";
       foreach(  $row as $key=>$value )
       {
           $csv_output .= $comma . $value;
           $comma = ", ";
       }
       $csv_output .= "\n";
    }
    
    print $csv_output;
    exit;