1

I'm using HttpURLConnection in Java (not Javascript) to call a PHP file which looks up a field in a mySQL database. In PHP how do I return the field contents (String) and in Java how do I receive them? Thank you. Awesome points to any who can help. Lol.

Sample code:

Java:

import java.net.HttpURLConnection;
import java.net.URL;
...
public static void Connect(String address){
    URL url = new URL("http://www.foo.com/getInfo.php?id=203&user=johndoe);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    String = ??? // What do I need to do to get the string from the PHP file?
    con.disconnect();
}

PHP:

<?php

$theFile = "../db_user_password";
$f = fopen($theFile, 'r') or die("Could not access password file.");
$user = chop(fgets($f));
$pass = chop(fgets($f));
$name = chop(fgets($f));
if (strlen($name) == 0) {
    $name = 'some_db';
}
fclose($f);

$connect = mysql_pconnect("localhost", $dbuser, $dbpass) or
        die('Could not connect: ' . mysql_error());

mysql_select_db($name, $connect) or die("Could not find database");

$id = urldecode($_GET['id']);
$user = urldecode($_GET['user']);

$query = "SELECT data FROM autosave_table WHERE id='$id' AND user='$user';
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

$updateQuery = "";
if ($num_rows == 1) {
    //What do I put here to return a string from data?
}

mysql_query($updateQuery);

?>
4

2 に答える 2