2016-04-20 176 views
-2

使用下面的代碼,我總是得到結果爲false,雖然我已經確認數據在表中。php mysql查詢總是返回false

我的PHP錯了嗎?

<?php 
    $con = mysqli_connect(".....", "....", ".....", "Pi"); 

    $username = $_POST["username"]; 
    $password = $_POST["password"]; 

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?"); 
    mysqli_stmt_bind_param($statement, $username, $password); 
    mysqli_stmt_execute($statement); 

    mysqli_stmt_store_result($statement); 
    mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password); 

    $response = array(); 
    $response["success"] = false; 

    while(mysqli_stmt_fetch($statement)){ 
     $response["success"] = true; 
     $response["name"] = $name; 
     $response["age"] = $age; 
     $response["username"] = $username; 
     $response["password"] = $password; 
    } 

    echo json_encode($response); 
?> 

使用下面的鏈接進行測試:http://joemalloy.co.uk/baj/Login.php?username=j&password=j

Database

在我的Android應用程序,使用以下:

登錄請求

import com.android.volley.Response; 
import com.android.volley.toolbox.StringRequest; 

import java.util.HashMap; 
import java.util.Map; 

public class LoginRequest extends StringRequest { 
    private static final String LOGIN_REQUEST_URL = "http://joemalloy.co.uk/baj/Login.php"; 
    private Map<String, String> params; 

    public LoginRequest(String username, String password, Response.Listener<String> listener) { 
     super(Method.POST, LOGIN_REQUEST_URL, listener, null); 
     params = new HashMap<>(); 
     params.put("username", username); 
     params.put("password", password); 
    } 

    @Override 
    public Map<String, String> getParams() { 
     return params; 
    } 
} 

登錄活動

import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 

public class LoginActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
     final EditText etPassword = (EditText) findViewById(R.id.etPassword); 
     final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink); 
     final Button bLogin = (Button) findViewById(R.id.bSignIn); 

     tvRegisterLink.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class); 
       LoginActivity.this.startActivity(registerIntent); 
      } 
     }); 

     bLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final String username = etUsername.getText().toString(); 
       final String password = etPassword.getText().toString(); 

       // Response received from the server 
       Response.Listener<String> responseListener = new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
          JSONObject jsonResponse = new JSONObject(response); 
          boolean success = jsonResponse.getBoolean("success"); 

          if (success) { 
           String name = jsonResponse.getString("name"); 
           int age = jsonResponse.getInt("age"); 

           Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class); 
           intent.putExtra("name", name); 
           intent.putExtra("age", age); 
           intent.putExtra("username", username); 
           LoginActivity.this.startActivity(intent); 
          } else { 
           AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this); 
           builder.setMessage("Login Failed") 
             .setNegativeButton("Retry", null) 
             .create() 
             .show(); 
          } 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }; 

       LoginRequest loginRequest = new LoginRequest(username, password, responseListener); 
       RequestQueue queue = Volley.newRequestQueue(LoginActivity.this); 
       queue.add(loginRequest); 
      } 
     }); 
    } 
} 
+0

'我的php出錯了嗎?是的,你沒有檢查返回值 –

+1

*「使用以下鏈接測試:http://joemalloy.co.uk/baj/Login.php?username=j&password= j'「* - 並且您正在使用POST數組。這是一種GET方法,奇怪的是,錯誤報告在這裏甚至沒有幫助。 –

+0

對不起,應該添加更多的內容。這些調用來自Android應用程序。 PHP不是我的首選語言,因此我最終可能會將其移至SOAP Web服務。 – JoeM

回答

0

您不知道您的查詢是否正在執行。 嘗試改用:

if(mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?")){....execute query and store results.....} 
else{ echo "Error"; } 

從我所看到的,有在這一行中有語法錯誤。

mysqli_stmt_bind_param($statement, $username, $password); 

您需要指定字符串類型。瞧here 你應該改變,要

mysqli_stmt_bind_param($statement, 'ss', $username, $password); 

還要檢查你的日誌,應該有針對此問題的警告或錯誤。

+0

'else {echo「Error」; }'這不會幫助他們。這將http://php.net/manual/en/mysqli.error.php –

+0

是的,你應該使用mysqli_error找出錯誤信息。我試圖做的一點是,你可以確定查詢是否執行。 – Silencer310

+0

我明白你的意思。 –