Android, php, MySQL, and phpMyAdmin

Jonathan Perez

Hoping you guys can shed some light. I'm working on an Android Capstone project that is connecting to a MySQL DB through php and phpMyAdmin. I'm trying to pass three parameter (childId, date1, and date5) from Android to the php script and it's not working. Logcat just shows Error when I try and print out the result. I think the issue is in my php script because everything works if I only pass 2 parameters (childId and date1). Any help is greatly appreciated. Here is my code.php first:

<?php
require "conn.php";

$response = array();

if($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$childId = $_POST["childId"];
$date1 = $_POST["date"];
$date5 = $_POST["date"];


$sql = "SELECT * FROM child_sessions WHERE childId like '$childId' AND date BETWEEN '$date1' AND '$date5'";


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

if($result->num_rows > 0) {

    $response["child_sessions"] = array();

    while($row = mysqli_fetch_array($result)) {
        $child_sessions = array();
        $child_sessions["date"] = $row["date"];
        $child_sessions["timeIn"] = $row["timeIn"];
        $child_sessions["timeOut"] = $row["timeOut"];
        $child_sessions["duration"] = $row["duration"];
        $child_sessions["childId"] = $row["childId"];
        $child_sessions["sessionCost"] = $row["sessionCost"];           
        array_push($response["child_sessions"], $child_sessions);
    }

    // success
    $response["success"] = 1;

    echo json_encode($response);
} else {
    echo "Error";
}

$conn->close();
?>

And here's my connection from Android:

private String childSessions(String childId, String date1, String date5) {

    InputStream inputStream = null;
    String line = null;

    Log.d(TAG, "childSessions: " + childId);
    Log.d(TAG, "childSessions: " + date1);
    Log.d(TAG, "childSessions: " + date5);
    try {
        String login_url = Constants.RETRIEVE_CHILD_SESSION_FOR_WEEK;
        URL url = new URL(login_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod(Constants.REQUEST_METHOD);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("childId", "UTF-8") + "=" + URLEncoder.encode(childId, "UTF-8") + "&"+
                URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date1, "UTF-8") + "&" +
                URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date5, "UTF-8");
        bufferedWriter.write(post_data);

        Log.d(TAG, "childSessions: " + post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

        Log.d(TAG, "childSessions: " + bufferedReader);

        String result = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
            Log.d(TAG, "childSessions: " + result);
        }
        bufferedReader.close();
        //result += System.getProperty("line.separator") + responseOutput.toString();

        inputStream.close();
        httpURLConnection.disconnect();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Thanks in advance.

YvesLeBorg

You are encoding a single 'date' parameter twice (and reading it twice in php). Fix your php and java to have the appropriate parameter keys date1 and date5.

so, in php script :

$date1 = $_POST["date1"];
$date5 = $_POST["date5"];

and in java , change the line to :

String post_data = URLEncoder.encode("childId", "UTF-8") + "=" + URLEncoder.encode(childId, "UTF-8") + "&"+
            URLEncoder.encode("date1", "UTF-8") + "=" + URLEncoder.encode(date1, "UTF-8") + "&" +
            URLEncoder.encode("date5", "UTF-8") + "=" + URLEncoder.encode(date5, "UTF-8");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Query behaves strange with php while working in phpmyadmin and mysql console.

From Dev

php, mysql and phpmyadmin, MAMP or homebrew

From Dev

MySQL SELECT query works in PHPmyadmin, not in PHP

From Dev

MySQL query works in PHPMyAdmin but not PHP

From Dev

PHP MYSQL query won't work in PHP but works in PHPMyAdmin

From Dev

MySQL in PHP gives error, MySQL in phpmyadmin does not

From Dev

Mysql query works in Phpmyadmin but not works in PHP

From Dev

MySQL query runs ok in phpMyAdmin but hangs in PHP

From Dev

MySQL single SELECT showing error in PHP but not in PHPMYADMIN

From Dev

Adding user to MySQL database in php using phpMyAdmin

From Dev

Inserting into mySQL with PHP but nothing shows up in phpmyadmin

From Dev

MySQL - Why is phpMyAdmin extremely slow with this query that is super fast in php/mysqli?

From Dev

mySQL inserts failing in PHP but not in phpmyadmin

From Dev

Mysql inserts blank field from php script, but not in phpmyadmin?

From Dev

MySQL INSERT query works in Phpmyadmin but not in PHP

From Dev

php, mysql and phpmyadmin, MAMP or homebrew

From Dev

DIfferent MySQL behaviour from MySQL query from PHP and directly at PHPMyAdmin

From Dev

MySQL in PHP gives error, MySQL in phpmyadmin does not

From Dev

MySQL query working in PHPMyAdmin but not working in PHP

From Dev

How to update apache, mysql, phpmyadmin and php on Raspbian?

From Dev

mysql and php %like% query. Works in phpmyadmin but different results php

From Dev

MySQL query runs ok in phpMyAdmin but hangs in PHP

From Dev

mysql query works in phpmyadmin but errors in php

From Dev

MySQL / PHPMyAdmin

From Dev

Adding user to MySQL database in php using phpMyAdmin

From Dev

Inserting into mySQL with PHP but nothing shows up in phpmyadmin

From Dev

Android, php, MySQL, and phpMyAdmin

From Dev

PHP and MySQL french accent works in PHPMyAdmin but not in page

From Dev

MySQL UPDATE works in phpMyAdmin but not in PHP

Related Related

  1. 1

    Query behaves strange with php while working in phpmyadmin and mysql console.

  2. 2

    php, mysql and phpmyadmin, MAMP or homebrew

  3. 3

    MySQL SELECT query works in PHPmyadmin, not in PHP

  4. 4

    MySQL query works in PHPMyAdmin but not PHP

  5. 5

    PHP MYSQL query won't work in PHP but works in PHPMyAdmin

  6. 6

    MySQL in PHP gives error, MySQL in phpmyadmin does not

  7. 7

    Mysql query works in Phpmyadmin but not works in PHP

  8. 8

    MySQL query runs ok in phpMyAdmin but hangs in PHP

  9. 9

    MySQL single SELECT showing error in PHP but not in PHPMYADMIN

  10. 10

    Adding user to MySQL database in php using phpMyAdmin

  11. 11

    Inserting into mySQL with PHP but nothing shows up in phpmyadmin

  12. 12

    MySQL - Why is phpMyAdmin extremely slow with this query that is super fast in php/mysqli?

  13. 13

    mySQL inserts failing in PHP but not in phpmyadmin

  14. 14

    Mysql inserts blank field from php script, but not in phpmyadmin?

  15. 15

    MySQL INSERT query works in Phpmyadmin but not in PHP

  16. 16

    php, mysql and phpmyadmin, MAMP or homebrew

  17. 17

    DIfferent MySQL behaviour from MySQL query from PHP and directly at PHPMyAdmin

  18. 18

    MySQL in PHP gives error, MySQL in phpmyadmin does not

  19. 19

    MySQL query working in PHPMyAdmin but not working in PHP

  20. 20

    How to update apache, mysql, phpmyadmin and php on Raspbian?

  21. 21

    mysql and php %like% query. Works in phpmyadmin but different results php

  22. 22

    MySQL query runs ok in phpMyAdmin but hangs in PHP

  23. 23

    mysql query works in phpmyadmin but errors in php

  24. 24

    MySQL / PHPMyAdmin

  25. 25

    Adding user to MySQL database in php using phpMyAdmin

  26. 26

    Inserting into mySQL with PHP but nothing shows up in phpmyadmin

  27. 27

    Android, php, MySQL, and phpMyAdmin

  28. 28

    PHP and MySQL french accent works in PHPMyAdmin but not in page

  29. 29

    MySQL UPDATE works in phpMyAdmin but not in PHP

HotTag

Archive