title: PHP MySQL JSON Login System
body: <p>I'm currently working on a login system (which works great by using two text fields and the user is then redirected). However, as I am developing a mobile app, it would be much easier to do this in JSON, and I am not entirely sure where to start. What I am basically looking to do is to use a https post request (from my app) so it would request: <a href="https://www.example.com/login.php?username=username&amp;password=password" rel="nofollow">https://www.example.com/login.php?username=username&amp;password=password</a> </p>
  
  <p>I have created a basic sample, but it's not what I'm looking to do with regards to passing in a parameter via the URL as my code just currently looks for users in the MySQL database and outputs the users in JSON. What I want to be able to do is pass in the username &amp; password parameters, via the URL, and then output a "success" or "error" JSON response if the username/password is in the database or not. </p>
  
  <pre><code>&lt;?php
  $host     = "localhost";
  $username = "root";
  $password = "password";
  $db_name  = "test";
  $con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
  mysql_select_db("$db_name") or die("cannot select DB");
  $sql    = "select * from test_table";
  $result = mysql_query($sql);
  $json   = array();
  if (mysql_num_rows($result))
      {
        while ($row = mysql_fetch_row($result))
         {
      $json['items'] = $row;
         }
      }
  mysql_close($db_name);
  echo json_encode($json, JSON_PRETTY_PRINT);
  ?&gt;
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I have now started working on another system, which is pretty much what I am looking to do (after I found an example here: <a href="http://www.dreamincode.net/forums/topic/235556-how-to-create-a-php-login-with-data-from-mysql-database/" rel="nofollow">http://www.dreamincode.net/forums/topic/235556-how-to-create-a-php-login-with-data-from-mysql-database/</a>) except every time that I request something like <a href="https://www.example.com/login.php?username=root&amp;password=password" rel="nofollow">https://www.example.com/login.php?username=root&amp;password=password</a> - I get the JSON error 1 code</p>
  
  <pre><code>&lt;?php
  $host = "localhost"; // Host name
  $db_username = "root"; // Mysql username
  $db_password = "password"; // Mysql password
  $db_name = "test"; // Database name
  $tbl_name = "test_table"; // Table name
  
  // Connect to server
  
  mysql_connect("$host", "$db_username", "$db_password") or die("cannot connect");
  
  // Select the database
  
  mysql_select_db("$db_name") or die("cannot select DB");
  
  // Get the login credentials from user
  
  $username = $_POST['username'];
  $userpassword = $_POST['password'];
  
  // Secure the credentials
  
  $username = mysql_real_escape_string($_POST['username']);
  $userpassword = mysql_real_escape_string($_POST['password']);
  
  // Check the users input against the DB.
  
  $query = "SELECT * FROM test_table WHERE user = '$username' AND password = '$userpassword'";
  $result = mysql_query($query) or die("Unable to verify user because " . mysql_error());
  $row = mysql_fetch_assoc($result);
  
  if ($row['total'] == 1) {
  
      // success
  
      $response["success"] = 1;
  
      // echoing JSON response
  
      echo json_encode($response);
  }
  else {
  
      // username and password not found
      // failed
  
      $response["failed"] = 1;
  
      // echoing JSON response
  
      echo json_encode($response);
  }
  
  ?&gt;
  </code></pre>