我正在使用Android项目,我需要使用PHP连接MySQL。为此,我使用PHP post方法将数据从Android传递到PHP。我的代码如下:
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data=URLEncoder.encode("mobile","UTF-8")+"="+URLEncoder.encode(mobile,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
// Toast.makeText(context,"after data pass",Toast.LENGTH_LONG).show();
bufferedWriter.flush();
bufferedWriter.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line=bufferedReader.readLine())!=null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
但问题出在PHP脚本中。没有数据通过,而PHP脚本收到一个空字符串。为什么会这样?那应该是什么解决方案? 我的PHP代码是:
<?php
require "conn1.php";
$mobile= $_POST["mobile"];
$user_password= $_POST["password"];
$query="select * from pharmacytable where mobile like '$mobile' and PharPassword like '$user_password';";
$result=mysqli_query($conn, $query);
if(mysqli_num_rows($result)>0){
echo "Login success";
} else{
echo "Login failed";
}
?>