![](/img/trans.png)
[英]How to pass variables from Android to be read by a PHP file, which then reads MYSQL Database and returns other data?
[英]How to pass MySQL database connection data in namevaluepairs from Android to PHP
我正在嘗試使用PHP連接到MySQL,並從Android傳遞數據庫連接參數。 我不想對連接參數進行硬編碼,也不想將它們存儲在單獨的文件中。 當我在PHP中具有數據庫參數時,我的代碼工作正常,但由於嘗試使用名稱值對將它們從Java傳遞到PHP,因此現在無法正常工作,如下所示。
除了使用傳遞的變量而不是進行硬編碼的PHP連接之外,其他都沒有改變,因此我懷疑存在格式或REGEX問題,但是找不到任何解決方案。 任何幫助,不勝感激!
在VolkerK的協助下解決的問題。 查看原始的PHP代碼並在下面進行更新。
原始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 = 'myname@gmail.com'";
//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
等等
在PHP文件中使用POST而不是REQUEST
您需要
更多
錯誤處理。 任何mysql_ *函數都可能失敗,並且您的代碼必須對此做出反應。
粗略的例子:
<?php
define('DEBUGLOG', true);
$output = array();
$mysql = mysql_connect($_REQUEST['url'],$_REQUEST['username'],$_REQUEST['password']);
if ( !$mysql ) {
$output['status']='error';
$output['errormsg']='database 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 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']='database 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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.