달달한 스토리

728x90
반응형

뜨문뜨문 글을 쓰게 된다.

 

요즘 급속도로 앱에 대해서 배우고 있는 중이다.

 

중요하게 생각되고, 다음에도 쓸 것 같은 기능을 위주로 글을 쓰려한다.

 

아무래도 시간은 유한하기 때문에 모든 정보를 쓰는 데에는

 

한계가 있음은 확실하다.

 

phpmyadmin 이란 sql을 편리하게 관리할 수 있는 php로 만들어진

 

그래픽 사용자 인터페이스이다(GUI).

 

한 달 전쯤 로그인 기능을 구현 중에 phpmyadmin을 다루는 법을 배워서 편하게 쓰게 되었다.

 

동시에 sql문으로 테이블도 짜면서, 한 층 더 성장하는 계기가 되었다.

 

오늘은 저번 시간에 배운 putty를 이용하여 AWS ec2 인스턴스를 만들고,

 

서버를 연동해서 phpmyadmin까지 만들어 보았다.

 

2021/02/28 - [Programing/AWS] - AWS와 안드로이드를 연결해보다 첫 시도!, AWS 가입 과정과 인스턴스 생성 및 puTTY설치 키 페어, ppk파일로 변경 및, phpmyadmin과 mySQL 설치 과정 정리 TIL #5

 

AWS와 안드로이드를 연결해보다 첫시도!, AWS 가입과정과 인스턴스 생성 및 puTTY설치 키페어, ppk파

드디어 글을 쓰게 된다. 저번 주부터 골머리를 앓아가지고.. 바로 이 녀석을 제대로 습득하지 못해서... 나 서버를 만들 수 있을까?.. 하는 걱정으로 이어지게 만든 AWS.. 이 아마존 웹 서비스에 대

daldalhanstory.tistory.com

그리고 그다음 과정인데, 어떻게 안드로이드에서 저 phpmyadmin서버와 연결하고, 데이터를

 

받아와서 활용할 수 있을까?

 

한 번 살펴보자.

 


살펴보기

{
    // The tab key will cycle through the settings when first created
    // Visit https://codexns.io/products/sftp_for_subime/settings for help
    
    // sftp, ftp or ftps
    "type": "sftp",

    "save_before_upload": true,
    "upload_on_save": true,
    "sync_down_on_open": false,
    "sync_skip_deletes": false,
    "sync_same_age": true,
    "confirm_downloads": false,
    "confirm_sync": true,
    "confirm_overwrite_newer": false,
    
    "host": "인스턴스 주소",
    "user": "ubuntu",
    //"password": "!",
    //"port": "80",
    
    "remote_path": "/var/www/html/",
    "ignore_regexes": [
        "\\.sublime-(project|workspace)", "sftp-config(-alt\\d?)?\\.json",
        "sftp-settings\\.json", "/venv/", "\\.svn/", "\\.hg/", "\\.git/",
        "\\.bzr", "_darcs", "CVS", "\\.DS_Store", "Thumbs\\.db", "desktop\\.ini"
    ],
    "file_permissions": "777",
    "dir_permissions": "777",
    
    //"extra_list_connections": 0,

    "connect_timeout": 30,
    //"keepalive": 120,
    //"ftp_passive_mode": true,
    //"ftp_obey_passive_host": false,
    "ssh_key_file": ".ppk경로",
    //"sftp_flags": ["-F", "/path/to/ssh_config"],
    
    //"preserve_modification_times": false,
    //"remote_time_offset_in_hours": 0,
    "remote_encoding": "utf-8",
    //"remote_locale": "C",
    //"allow_config_upload": false,
}

 

우선 시작하시기 전에 저번 글에서 sftp 설정을 모두 완료했다는 가정하에

 

진행이 되는 점 참고해주세요!

 

아직 못하신 분들은 위에 링크로 보고 오시면 됩니다.

 

 

 

우선 저번 시간에 포스팅해둔 phpmyadmin으로 들어간다.

 

먼저 이 phpmyadmin으로 데이터베이스에 테이블을 만들어 줄 것이다.

 

테이블은 sql문으로 작성할 수 있는데,

 

내가 만들 데이터베이스로 들어가 위에 보면 SQL이라고 써져있는 탭을 누르자.

 

그러면 이러한 화면을 볼 수 있다.

 

일단 예제를 만드는 것이므로 다음과 같이 입력해보자.

 

CREATE TABLE `USER` (
id VARCHAR(20) NOT NULL,
pw VARCHAR(20) NOT NULL,
mg VARCHAR(20) NOT NULL,
PRIMARY KEY(id));

 

이 SQL문을 해석해보면,

 

USER라는 테이블을 생성하고,

 

id라는 이름을 가진 문자열 20을 갖고, 널 값을 가지지 않는 칼럼을 갖고,

(나머지도 같은 뜻)

 

프라이머리 키로 id를 지정해준다.

 

프라이머리 키를 지정하면 식별하기 용이해진다고 한다.

 

이렇게 sql문을 작성하고, 오른쪽 아래 실행 버튼을 누르면,

 

 

이런 식으로 테이블을 작성한다. 자 그럼 이 안에다가

 

데이터를 넣고 그 데이터를 어떻게 가져오는지 알아보자.

 

우선 php문을 작성해보자.

 

저번 시간에 사용했던 sublimeText를 열어

 

두 가지 php 파일을 만들어주자.

 

Logtest.php

<?php

    $con = mysqli_connect("localhost", "아이디", "비번", "데이터베이스이름");
    mysqli_query($con,'SET NAMES utf8');


    $id = $_POST["id"];
    $pw = $_POST["pw"];

    
    $statement = mysqli_prepare($con, "SELECT * FROM USER WHERE id = ? AND pw = ?");
    mysqli_stmt_bind_param($statement, "ss", $id, $pw);
    mysqli_stmt_execute($statement);


    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $id, $pw, $mg);

    $response = array();
    $response["success"] = false;
 
    while(mysqli_stmt_fetch($statement)) {
        $response["success"] = true;
        $response["id"] = $id;
        $response["pw"] = $pw;
        $response["mg"] = $mg;
    }

    echo json_encode($response);

?>

간단히 설명하자면 우선 mmysqli_connect안에 들어가는 내용은,

 

아이피, phpmyadmin아이디와 비밀번호, 그리고 그 안에 데이터베이스에 이름을 입력하여,

 

접속하게 만들어준다.

 

그리고 $_POST는 포스트 방식으로 서버에서 값을 받는다는 뜻이다.

 

한마디로 id, pw 값을 받는다는 뜻,

 

그리고 SELECT * FROM USER WHERE id =? AND pw = ?

 

이 뜻은 sql문인데, 지금 가져온 id와 비밀번호가 존재하는지 안 존재하는지,

 

참인지 거짓인지 판단을 해주는 sql문이다.

그 아래에는 s는 string, int는 i로 넣어주며

 

우리는 String형식으로 두 개를 넣었기 때문에 ss라고 썼다.

 

"ss", $id, $pw

 

 

 

이곳에도 필히 mg를 넣어줄 것.

 

mysqli_stmt_bind_result($statement, $id, $pw, $mg);

 

 

 

아래 이것은, 아이디와 비밀번호에서 success값을 받을 때 얻게 되는 정보들이다.

 

while(mysqli_stmt_fetch($statement)) {
        $response ["success"] = true;
        $response ["id"] = $id;
        $response ["pw"] = $pw;
        $response ["mg"] = $mg;
    }

 

 

 

이 정보들은 아래 Resitest에서 입력하게 될 것이다.

 

Resitest.php

 

<?php 
    $con = mysqli_connect("localhost", "아이디", "비번", "데이터베이스이름");
    mysqli_query($con,'SET NAMES utf8');

    $id = $_POST["id"];
    $pw = $_POST["pw"];
    $mg = $_POST["mg"];
    

    $statement = mysqli_prepare($con, "INSERT INTO USER VALUES (?,?,?)");
    mysqli_stmt_bind_param($statement, "sss", $id, $pw, $mg);
    mysqli_stmt_execute($statement);


    $response = array();
    $response["success"] = true;
 
    echo json_encode($response);
?>

 

여기도 비슷하다.

 

다른 점은 INSERT INTO USER VALUES (?,?,?) 

 

이 sql 문인데, 이 서버에 이 값들을 각 칼럼명에다가 입력하겠다는 뜻이다.

 

? 표는 입력할 개수만큼 쓰고,

 

bind_param도 위에 Logtest와 같다.

 

그럼 이제 안드로이드로 넘어가 보자.

 

우선 서버로부터 json데이터로 받고 보내기

 

편리하게 볼리 라이브러리를 쓰겠다.

 

build.app 다음 라이브러리를 설치하자.

 

 implementation 'com.android.volley:volley:1.1.1'

그리고 AndroidManifest.xml에 인터넷 권한을 허용해야 한다.

 

<uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:usesCleartextTraffic="true"
        ....>
        
        .....application/>

 

 

그다음은 

 

우선 입력을 해서 서버로 전송하는 액티비티이다.

 

Register.java

 

public class Register extends AppCompatActivity {
    EditText textID, textPW, message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Button button = findViewById(R.id.button);
        textID = findViewById(R.id.edittext);
        textPW = findViewById(R.id.edittext2);
        message = findViewById(R.id.mgText);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = textID.getText().toString(); //텍스트뷰에서 아이디값을 가져온다.
                String pw = textPW.getText().toString(); //텍스트뷰에서 비밀번호값을 가져온다.
                String Emessage = message.getText().toString(); //텍스트뷰에서 메세지를 가져온다.

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            boolean success = jsonObject.getBoolean("success");
                            if (success) {//성공시
                                Toast.makeText(getApplicationContext(), "성공", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(Register.this, Login.class); //로그인 객체로 가기
                                startActivity(intent);

                            } else {//실패시

                                Toast.makeText(getApplicationContext(), "실패", Toast.LENGTH_SHORT).show();
                                return;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "예외 1", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(id, pw, Emessage, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Register.this);
                queue.add(registerRequest);

            }
        });
    }
}

ㅁㅁ

 

만약 저 EditText에 정보를 입력하고, 버튼을 클릭하면,

 

서버로 저 데이터들이 전송이 되고,

 

성공 시 토스트 메시지로 성공이라고 표시되고, Login, java 액티비티로 넘어가게 된다.

 

activity_register.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Register">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="아이디"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.272" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:text="비밀번호"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회원가입"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/mgText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="95dp"
        android:layout_marginEnd="97dp"
        android:layout_marginRight="97dp"
        android:layout_marginBottom="46dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="보낼메세지"
        app:layout_constraintBottom_toTopOf="@+id/edittext"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

그리고 서버로 요청하는 StringRequest문을 작성한다.

 

RegisterRequest.java

 

public class RegisterRequest extends StringRequest {

    // 서버 URL 설정 (PHP 파일 연동)
    final static private String URL = "http:아마존 인스턴스 DNS 주소(혹은 웹호스팅서버)/Resitest.php"; //호스팅 주소 + php
    private Map<String, String> map;



    public RegisterRequest(String id, String pw, String mg, Response.Listener<String> listener) { //문자형태로 보낸다는 뜻
        super(Method.POST, URL, listener, null);

        map = new HashMap<>();
        map.put("id", id);
        map.put("pw", pw);
        map.put("mg", mg);

    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return map;
    }
}

서버로 입력한 id, pw, mg 값을 맵핑하여 보낸다.

 

이렇게 하면, 테이블에 우리가 입력한 정보가 

 

칼럼 명마다 입력되게 된다.

 

확인해보자.

 

 

 

이런 식으로 컬럼에 내가 입력한 데이터들이 컬럼에 입력되었다.

 

이런식으로 값을 저장했는데,

 

우리가 아이디나 비밀번호를 찾는 경우에 저 데이터를 참고해서,

 

값을 가져와야 한다.

 

데이터를 가져와보자.

 

우선 안드로이드에서 데이터를 받아보자.

 

Register.java와 비슷하다.

 

Login.java

 

public class Login extends AppCompatActivity {

    EditText textID, textPW;
    Button buttonBringit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        textID = findViewById(R.id.edittext3);
        textPW = findViewById(R.id.edittext4);

        buttonBringit = findViewById(R.id.button2);



        buttonBringit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String Lid = textID.getText().toString();
                String Lpw = textPW.getText().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            boolean success = jsonObject.getBoolean("success");
                            if (success) {//성공시

                                String mg = jsonObject.getString("mg"); //전달받은 json객체에서 내가 넣어둔 mg를 꺼내기

                                Toast.makeText(getApplicationContext(), "메세지 : " + mg, Toast.LENGTH_SHORT).show();

                            } else {//실패시

                                Toast.makeText(getApplicationContext(), "실패", Toast.LENGTH_SHORT).show();
                                return;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "예외 1", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                };

                LoginRequest loginRequest = new LoginRequest(Lid, Lpw, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Login.this);
                queue.add(loginRequest);

            }
        });
    }
}

위에서 보면 저 jsonObject에서 받아오는 것에 성공했다면,

 

mg를 받아올 수 있다.

 

그리고 그 결과는 Toast값으로 나오게 해 두었다.

 

아까 위에 작성했던 php문으로 인해 

 

아이디와 비밀번호가 일치해야 저 데이터를 가져올 수 있는 것이다.

 

나머지 xml문과 LoginRequest문이다.

 

LoginRequest.java

public class LoginRequest extends StringRequest {

    // 서버 URL 설정 (PHP 파일 연동)
    final static private String URL = "아마존 인스턴스 DNS 주소/Login.php"; //호스팅 주소 + php
    private Map<String, String> map;



    public LoginRequest(String id, String pw, Response.Listener<String> listener) { //문자형태로 보낸다는 뜻
        super(Method.POST, URL, listener, null);

        map = new HashMap<>();
        map.put("id", id);
        map.put("pw", pw);

    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return map;
    }
}

 

 

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Login">

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="아이디"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.316" />

    <EditText
        android:id="@+id/edittext4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="284dp"
        android:layout_marginBottom="30dp"
        android:text="비밀번호"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="가져오기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.578" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

한번 결과를 보겠다.

 

 

이런 식으로 틀리면, 값을 가져오지 못한다.

 

즉, 입력된 아이디와 비밀번호가 일치해야,

 

메시지를 가져온다는 뜻이다.

 

 

마지막으로 보너스로 

 

 $statement = mysqli_prepare($con, "SELECT * FROM USER WHERE id =? AND pw =?");

 

php에서 이 부분을 잘 이용하면,

 

상황에 따라 대처해서 사용할 수 있다.

 

위와 같은 경우는 아이디와 비밀번호가 일치하는지 안 하는지 찾는 것이고,

 

INSERT INTO USER VALUES (?,?,?,?,?,?,?,?,?)

 

이건 아까 위에 사용했던 것처럼 데이터베이스에 입력을 하는 sql문이다.

 

"UPDATE USER SET pw = '$newpw' WHERE id = '$Eid'"

 

이것은 이제 비밀번호나 다른 사항을 변경할 때 쓰는 sql문이다.

 

사실 나도 요정도만 사용하고 있어서 하하...

 

나머지는 인터넷 검색으로 많이 나온다.

 

오늘 공부한 자료는 깃 헙에 올려놓았다.

 

php 파일은 위에 것을 복사해서 만드는 것이 좋을 듯하다.

 

여기서 글을 마치겠다.

 

https://github.com/qjsqjsaos/Tistory6

 

qjsqjsaos/Tistory6

볼리라이러리를 이용하여 php를 이용해 간단한 로그인과 회원가입 구현. phpmyadmin으로 sql문을 사용하여 테이블만들고, 값 저장, 데이터가져오기 - qjsqjsaos/Tistory6

github.com

 

 

 

 

728x90
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading