我正嘗試使用PHP連接到MySQL,從Android傳遞數據庫連接參數。我不想對連接參數進行硬編碼,也不想將它們存儲在單獨的文件中。當我在PHP中有數據庫參數時,我的代碼工作正常,但現在不能工作,因爲我試圖通過名稱值對將它們從Java傳遞到PHP,如下所示。如何將namevaluepairs中的MySQL數據庫連接數據從Android傳遞到PHP
除了使用傳遞變量而不是硬編碼的PHP連接外沒有任何變化,所以我懷疑有些格式化或REGEX問題,但找不到任何解決方案。任何幫助非常感謝!
根據VolkerK的協助解決問題。查看原始的PHP代碼並在下面更新。
ORIGINAL SQLQuery.php:
<?php
mysql_connect($_REQUEST['url'],$_REQUEST['username'],$_REQUEST['password']);
mysql_select_db($_REQUEST['database']);
$q=mysql_query($_REQUEST['SQL']);
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
工作SQLQuery.php:
<?php
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
define('DEBUGLOG', true);
$output = array();
$mysql = mysql_connect($_REQUEST['url'],$_REQUEST['username'],$_REQUEST['password']);
if (!$mysql) {
$output['status']='Error';
$output['errormsg']='MySQL connect error';
if (defined('DEBUGLOG') && DEBUGLOG) {
$output['errordetails'] = array(
'msg'=>mysql_error(),
'url'=>$_REQUEST['url'],
'username'=>$_REQUEST['username'],
'password'=>$_REQUEST['password']
);
}
}
else if (!mysql_select_db($_REQUEST['database'])) {
$output['status']='Error';
$output['errormsg']='Database select error';
if (defined('DEBUGLOG') && DEBUGLOG) {
$output['errordetails'] = array(
'msg'=>mysql_error(),
'url'=>$_REQUEST['url'],
'database'=>$_REQUEST['database']
);
}
}
else if (false===($q=mysql_query($_REQUEST['SQL']))) {
$output['status']='Error';
$output['errormsg']='Query error';
if (defined('DEBUGLOG') && DEBUGLOG) {
$output['errordetails'] = array(
'msg'=>mysql_error(),
'url'=>$_REQUEST['url'],
'SQL'=>$_REQUEST['SQL']
);
}
}
else {
while($e=mysql_fetch_assoc($q)) {
$output[]=$e;
}
}
print(json_encode($output));
從我的Android代碼(!細節改變,以保護無辜)提取物:
String phpDBURL = "mysqlserver.blah.com:3306";
String phpURL = "http://www.blah.com/php/";
String dbname ="dbref_Evaluate";
String username = "dbref_admin";
String password = "password";
String SQL = "SELECT ID, ShortDesc FROM User WHERE Account = '[email protected]'";
//the query to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("url",phpDBURL));
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
nameValuePairs.add(new BasicNameValuePair("database",dbname));
nameValuePairs.add(new BasicNameValuePair("SQL",SQL));
Log.v("Common.SQLQuery", "Passing parameters: " + nameValuePairs.toString());
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(phpURL + "SQLQuery.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//convert response to string
等等。
'mysql_'將被棄用,從PHP,http://www.php.net/manual/en/faq.databases.php#faq.databases除去。 mysql.deprecated。學習使用PDO,而不是http://anuary.com/54/input-sanitization-and-escaping-for-database-and-stdout-using-php – Gajus 2012-08-02 12:37:23