0

I have an 'Android' project where I need to fetch some information from a database. The database is 'MySQL'. I have to go throught a webservice and as I have never done any PHP before now, I hardly see the problem as the logic seems ok to me.

Here is my code:

<?php

        require 'connect.php';

        $username = 'alex@hotma';
        $password = 'soleil';

        $sql = 'SELECT ID, NAME, PASSWORD, EMAIL FROM Account';
        #$sql = 'CALL Login('.$username .', '. $password .')';

        $result = $conn->query($sql);

        $response = array();

        if (!empty($result))
        {
            if($result->num_rows > 0) 
            {
                $response['success'] = 1;
                $users = array();

                while($row = $result->fetch_assoc()) 
                {
                    $user = array();
                    $user["id"] = $row["ID"];
                    $user["name"] = $row["NAME"];
                    $user["password"] = $row["PASSWORD"];
                    $user["email"] = $row["EMAIL"];

                    //Trying to add the user into my users array

                    //If i uncomment this line everything is shown, but not in the response array
                    //echo json_encode($user);
                    array_push($users, $user);
                }
                $response['users'] = $users;
            }
            else
            {
                $response['success'] = 0;
                $response['message'] = 'No user found';
            }
        }
        else
        {
            $response['success'] = 0;
            $response['message'] = 'No user found';
        }

        echo json_encode($response);
        $conn->close();
?>

I currently have more than 6 users in my database, but i cannot seems to get them all. I have left some of my code commented so you can see what i have tried, alas without any success. I want to return a 'JSON' array with all my user inside it.

Do any of you have an idea on how to proceed?

Chax
  • 1,041
  • 2
  • 14
  • 36

2 Answers2

1

That works IMHO

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include"config.inc.php";

$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");

if (mysqli_connect_errno()) { echo "Error: " . mysqli_connect_error($mysqli); }

$query = " SELECT ID, NAME, PASSWORD, EMAIL FROM Account ";
$stmt1 = $mysqli->prepare($query);

$results = $stmt1->execute();
$stmt1->bind_result($ID, $NAME, $PASSWORD, $EMAIL);
$stmt1->store_result();

  if ($stmt1->num_rows > 0) {
  $users = array();
  $user = array();
  while($stmt1->fetch()){
    echo"[ $ID / $NAME / $PASSWORD / $EMAIL ]<br />";

    $user["ID"] = "$ID";
    $user["NAME"] = "$NAME";
    $user["PASSWORD"] = "$PASSWORD";
    $user["EMAIL"] = "$EMAIL";

    array_push($users, $user);
  }
}
else
{ echo"[ no data ]"; }

print_r($users);
echo json_encode($users);

?>
OldPadawan
  • 1,247
  • 3
  • 16
  • 25
  • could you explain whats the main difference between my code and your code? I see you use statement, waht are the main benefirt of this approach? – Chax Apr 06 '17 at 20:49
  • prepared statements make it easier to read / maintain and are more safe in many ways for other purposes, such as INSERT / UPDATE and SELECT when using user input data (BTW: the code I posted works, I just tested it with a difference : the query -> SELECT id, nom, prenom FROM tbl_students LIMIT 3 (to match DB of mine) – OldPadawan Apr 06 '17 at 20:52
  • Too late to EDIT : and also, $users = array(); $user = array(); are both declared *before* loop, in order not to be overwritten/reset – OldPadawan Apr 06 '17 at 21:08
  • I fiddle with this code a bit after i ended my conversation with 'oldPadawan'. His solution worked as well as @victor. the problem was the charset encoding. I have read about it [here] (http://stackoverflow.com/a/19999720/3229805) and the solution was to simply add 'mysqli_set_charset($conn, 'utf8')' and everything ouputed perfectly. Both answer on this post are correct. – Chax Apr 07 '17 at 13:28
0

Im wondering if you have a scoping error, tbh im not totally up to date on php scoping rules so give this a try.

 <?php

            require 'connect.php';

            $username = 'alex@hotma';
            $password = 'soleil';

            $sql = 'SELECT ID, NAME, PASSWORD, EMAIL FROM Account';
            #$sql = 'CALL Login('.$username .', '. $password .')';

            $result = $conn->query($sql);

            $response = array();

             $users = array();// maybe you have scoping error, put this here
            if (!empty($result))
            {
                if($result->num_rows > 0) 
                {
                    $response['success'] = 1;
                    #$response['user'] = array();

                    while($row = $result->fetch_assoc()) 
                    {
                        $user = array();
                        $user["id"] = $row["ID"];
                        $user["name"] = $row["NAME"];
                        $user["password"] = $row["PASSWORD"];
                        $user["email"] = $row["EMAIL"];

                        #Trying to add the user into my users array
                        #$response['users'][] = $user;

                        #echo json_encode($user);
                       // array_push($users, $user);
                        $users[] = $user;
                    }
                }
                else
                {
                    $response['success'] = 0;
                    $response['message'] = 'No user found';
                }
            }
            else
            {
                $response['success'] = 0;
                $response['message'] = 'No user found';
            }

            $response['users'] = $users;
            $conn->close();

            return json_encode($response);
    ?>
victor
  • 802
  • 7
  • 12