我在 Java(不是 Javascript)中使用 HttpURLConnection 来调用一个 PHP 文件,该文件在 mySQL 数据库中查找一个字段。在 PHP 中如何返回字段内容(字符串),在 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);
?>