setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Run the selection query: $rows = $db->query("SELECT * from passwords WHERE username = '$username'"); // Run through each row in the results from the query: $results = []; foreach ($rows as $row) { // make an associative array out of the values in the row: $result = [ "username" => $row['username'], "password" => $row['password'], "website" => $row['website'], ]; // and add this result to the list we are going to print: $results[] = $result; } // set the header to let the client know what's coming back: header("Content-Type: application/json"); print(json_encode($results)); } catch (PDOException $ex) { print(json_encode([ "exception" => $ex->getMessage(), ])); }