Do I need remote login to can read and write from a MySQL database via a php script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Do I need remote login to can read and write from a MySQL database via a php script?



I am practicing creating an authentication system using Java, PHP, and SQL. I have one entry in the MySQL database, but my application refuses to sign in. I also created a register side of the application but it doesn't work either. So I was wondering if not having remote login access would affect it.


if ($_SERVER['REQUEST_METHOD']=='POST')

$matriculation_number = $_POST['matriculation_number'];
$password = $_POST['password'];

$conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general");

if ($conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general"))
$sql = "SELECT * FROM users_general WHERE matriculation_number='$matriculation_number' ";

$response = mysqli_query($conn, $sql);
$check = array();
$result = array();
$check['connection'] = array();
$result['login'] = array();

if ( mysqli_num_rows($response) === 1 )

$row = mysqli_fetch_assoc($response);

if ( password_verify($password, $row['password']) )

$index['matriculation_number'] = $row['matriculation_number'];
$index['first_name'] = $row['first_name'];
$index['middle_name'] = $row['middle_name'];
$index['last_name'] = $row['last_name'];
$index['gender'] = $row['gender'];
$index['level'] = $row['level'];
$index['programme'] = $row['programme'];
$index['department'] = $row['department'];
$index['college'] = $row['college'];

array_push($result['login'], $index);

$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result);

mysqli_close($conn);

else

$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result);

mysqli_close($conn);



else
$check['Connected'] = "Connection failed";
echo json_encode($check);





The volley code:


StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_REQUEST_URL,
new Response.Listener<String>()
@Override
public void onResponse(String response)
try
System.err.println("__________________________________________LOG________________________________________");
Log.i("tagconvertstr", "["+response+"]");
System.err.println("__________________________________________LOG________________________________________");
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("login");

if (success.equals("1"))
for (int i = 0; i < jsonArray.length(); i++)
JSONObject object = jsonArray.getJSONObject(i);

String matriculation_number = object.getString("matriculation_number").trim();
String first_name = object.getString("first_name").trim();
String middle_name = object.getString("middle_name").trim();
String last_name = object.getString("last_name").trim();
String gender = object.getString("gender").trim();
int level = object.getInt("level");
String programme = object.getString("programme").trim();
String department = object.getString("department").trim();
String college = object.getString("college").trim();

System.out.println(matriculation_number + "n" + first_name + "n" + middle_name + "n" +
last_name + "n" + gender + "n" + level + "n" + programme + "n" +department + "n" + college);
System.out.println("___________________________CHECK___________________________________");

String message = "Welcome " + matriculation_number;
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();

Intent l = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(l);
LoginActivity.this.finish();


catch (JSONException e)
e.printStackTrace();
String message = "Error " + e.toString();
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
pWord.setText("");
pWord.clearFocus();
uName.clearFocus();
login.setVisibility(View.VISIBLE);


,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
String message = "Error " + error.toString();
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
pWord.setText("");
pWord.clearFocus();
uName.clearFocus();
login.setVisibility(View.VISIBLE);


)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String, String> params = new HashMap<>();
params.put("matriculation_number", matriculation_number);
params.put("password", password);
return params;

;

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}



I was asked to post the script... which I have done. I have also added a check to make sure the script is connected to the database.





whats your error message
– NuOne T Attygalle
17 hours ago





success:0 message:error... It seems to be working fine but won't login.
– Beulah Akindele
17 hours ago






just send a post request to the url and see the output. you can use postman to send a post request to the url of the php file
– NuOne T Attygalle
17 hours ago






I used Volley to send the request... Isn't that pretty much the same?
– Beulah Akindele
17 hours ago






you have used volley to do post programmatically right ? i want you to manually send post with all your parameters to the php file.. then you can find out if the sever works .. and also can you list the volley code as well
– NuOne T Attygalle
17 hours ago




1 Answer
1



since the php file is responsible for connect to the database and look for use validation java doesn't have to have access for db for this to work. all you need to do is calling the php endpoint from java .. use some http library to do the post to the php file.



first see if the php file works right manually. you can send a post to the php file using postman.
https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests



then look at the output. if its ok then there may be an error in your java code.



and i think this line of code wont work


if ($conn = mysqli_connect("localhost", "id4922470_public", "password","id4922470_cuc_general"))




should replace with


if($conn)






I already use volley.
– Beulah Akindele
17 hours ago






After changing the if statement... I got this: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 19.
– Beulah Akindele
16 hours ago


mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 19





what is the output of postman... did you try to send post from postman
– NuOne T Attygalle
16 hours ago





It said undefined index for matriculation number and password, on lines 5 and 6 respectively. Along with the mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 19. Also, the connection seems to fail now: "connection":,"Connected":"Connection failed".
– Beulah Akindele
16 hours ago



mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 19


"connection":,"Connected":"Connection failed"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Trying to Print Gridster Items to PDF without overlapping contents