title: Trying to display User info from a database in my Android application
body: <p>I have been working on this for a while. The first thing I have tried is storing the logged in user in a session, and then trying to use that variable later such as the following: </p>
  
  <p><strong>Login.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
  
  $sessionid = session_id();
  
      require_once('connect.inc.php');
  
      $sql = "SELECT username, password FROM USER WHERE username = ?";
  
      $stmt = $conn-&gt;prepare($sql);
  
      $username = $_POST["username"];
      $password = $_POST["password"];
  
      $stmt-&gt;bind_param("s", $username);
  
      $stmt-&gt;execute();
  
      $stmt-&gt;bind_result($user, $pass);
  
      while($stmt-&gt;fetch()){
          $verify = password_verify($password, $pass);
      }
  
      if($verify){
          $_SESSION["username"] = $username;
          echo 'connected';
      echo $sessionid;
      }else{
          echo 'check details';
      }
  
      mysqli_close($conn);
  }
  ?&gt;
  </code></pre>
  
  <p>I am then taking the response of the login in message and splitting it into two variables. The login response and session id. I take the session id and store in shared preferences. I am trying to store the session ID in my java method so I can access the session user. Here is my java code for attempting to get the user:</p>
  
  <p><strong>GetUserData Java method</strong></p>
  
  <pre><code>private void getUserData() {
  
      SharedPreferences sharedPreferences = getSharedPreferences(Config.sharedPref, Context.MODE_PRIVATE);
      String sessionId = sharedPreferences.getString(Config.SID, "SessionID");
  
      StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetUserData.php?PHPSESSID=" + sessionId,
              new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
  
                      JSONObject jsonObject = null;
                      try {
                          //json string to jsonobject
                          jsonObject = new JSONObject(response);
  
                          //get json sstring created in php and store to JSON Array
                          result = jsonObject.getJSONArray(Config.json_array);
  
                          //get username from json array
                          getUserInfo(result);
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
  
                  }
              });
  
      RequestQueue requestQueue = Volley.newRequestQueue(this);
      requestQueue.add(stringRequest);
  }
  
  private void getUserInfo(JSONArray jsonArray){
      for(int i = 0; i &lt; jsonArray.length(); i++) {
          try {
              JSONObject json = jsonArray.getJSONObject(i);
  
              userInfo.add(json.getString(Config.getUsername));
          } catch (JSONException e) {
  
          }
      }
  }
  </code></pre>
  
  <p>Here is the php file the java method is attempting to call:</p>
  
  <p><strong>GetUserData.php</strong></p>
  
  <pre><code>&lt;?php
   session_start();
  if($_SERVER['REQUEST_METHOD'] == 'GET'){
  
              $username = $_SESSION['username'];
  
      $sql = "SELECT * FROM USER WHERE username = '$username'";
  
      require_once('connect.inc.php');
  
      $run = mysqli_query($conn, $sql);
      $result = array();
  
      while($row = mysqli_fetch_array($run)){
          array_push($result, array(
              'id' =&gt; $row['id'],
              'fname' =&gt; $row['fname'],
              'lname' =&gt; $row['lname'],
              'username' =&gt; $row['username'],
              'email' =&gt; $row['email'],
          ));
      }
  
      echo json_encode(array('result'=&gt;$result));
  
      mysqli_close($conn);
  }
  ?&gt;
  </code></pre>
  
  <p>When debugging, the 'result' array is empty, so for some reason, </p>
  
  <pre><code>      $sql = "SELECT * FROM USER WHERE username = '$username'";
  </code></pre>
  
  <p>is not working. I know it has something to do with sessions, but Im not sure where the problem is. </p>
  
  <p>My next attempt is going to try to just store the logged in user in shared preferences, and then call that variable from a php file and run a query to display user info with that variable. How would I do this? </p>
  
  <p>Thank you. </p>