-1

I have the following table in phpmyadmin.

The name of the table is data:

enter image description here

Im trying to use php file in order to obtain all of the rows where the user_id for example equals to 3 using prepared statements as follows:

<?php
session_start();

$DATABASE_HOST = "localhost";
$DATABASE_USER = "root";
$DATABASE_PASS = "XXX";
$DATABASE_NAME = "XXX";

$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);

if ($stmt = $con->prepare('SELECT code FROM data WHERE user_id = ?')) {

    $stmt->bind_param('s', $_POST['user_id']);
    $stmt->execute();
    $stmt->bind_result($col1);


    while ($row = $stmt->fetch()) {
        echo $col1;
    }

    $stmt->close();
}
$con->close();
?>

This code works and returns one long string that consists of all the codes.

My goal eventually is to obtain some array into java where each cell inside the array will have one code.

What is the best way to obtain from php an array that I will be able to use in java?

Thank you

benb
  • 351
  • 1
  • 2
  • 7

1 Answers1

0

I prefere to use JSON, its readable for the most programminglanguages:

//Make an array to collect the results
$rows = array();
while ($row = $stmt->fetch()) {
       //add the results to the array
       $rows[] = $col1;
   }
//print the object:
echo json_encode($rows);

This gives you an JSON-Array back. PHP.Net: json_encode

In your Java-application you can use also parse the JSON to an array or object Parse JSON in Java (Stackoverflow)