こんにちは私はAndroidを初めて使用し、ログイン画面を備えたアプリを開発しています。
多くのチュートリアルを試しましたが、mysqlデータベースをAndroidアプリに接続する方法がわかりません。すべてのチュートリアルで、彼らはphpを使用してAndroidアプリをデータベースに接続しました。
私が知りたいのは、phpファイルを正確にどこに配置する必要があるのか、そしてphpファイルをどこにどのように作成する必要があるのかということです。
mysqlワークベンチを使用してデータベースを作成しています。
私はEclipse4.2を使用してJavaコードを記述しています。
こんにちは私は次のコードを使用して、入力したユーザー名が正しいかどうかをphpコードと一緒にチェックしていますが、問題は常に間違ったユーザー名が表示されることです。
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://192.168.3.27/abc/check.php", postParameters); //Enetr Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
error.setText(res);
if(res.equals("1")){
Intent myIntent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(myIntent);
}
else
error.setText("Sorry!! Incorrect Username or Password");
これは私のphpコードです。
<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db
$user = ‘root’;
$pswd = ‘root’;
$db = ‘mylogin’;
$conn = mysql_connect(‘localhost’, $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = “SELECT * FROM userpass WHERE login_id = ‘$un’ AND login_pass = ‘$pw’”;
$result = mysql_query($query) or die(“Unable to verify user because : ” . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
echo 1; // for correct login response
else
echo 0; // for incorrect login response
?>