我的PHP文件login.php如下所示:
<?php
//if($_SERVER['REQUEST_METHOD']=='POST'){
//$username = $_POST['username'];
$username = "Dave";
require_once('dbConnect.php');
$sql = "SELECT * FROM user WHERE username = '$username'";
$result = mysqli_query($con,$sql);
$check = mysqli_fetch_array($result);
if(isset($check)){
echo 'Logged in';
} else {
echo 'Could not log in';
}
//}
?>
当我访问myserver.com/login.php时,我看到Logged in
,因为Dave在我的数据库中。
在我的Android应用中,我的Volley Log in按钮看起来像这样:
private void userLogin() {
username = editTextUsername.getText().toString().trim();
// password = editTextPassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equals("Logged in")){
Toast.makeText(LoginActivity.this,"yahoo, you're in",Toast.LENGTH_LONG).show();
openProfile();
}else{
Toast.makeText(LoginActivity.this,"sorry, you're out",Toast.LENGTH_LONG).show();
// Toast.makeText(LoginActivity.this,response,Toast.LENGTH_LONG).show();
}
}
}, etc... etc...
但是当我点击我的登录按钮时,我的祝酒词总是sorry, you're out
。你知道为什么吗?
答案 0 :(得分:0)
因为:
$check = mysqli_fetch_array($result);
该函数返回一个数组(成功)或布尔值false(失败)。无论哪种方式,$check
总是将被设置,因此isset($check)
将始终返回true。
你的逻辑应该是
if ($check === false) {
表示没有行(或查询失败)。