SQL -> run

create table student(

no int not null auto_increment,

id varchar(20) not null,

pwd varchar(20) not null,

name varchar(20),

phone varchar(20),

address varchar(150),

primary key(no)

) CHARSET=utf8;


insert into student(id, pwd, name, phone,address)

    values("1234567","1111","홍길동","053-123-1234","대구 수성구");

insert into student(id, pwd, name, phone,address)

    values("2222222","2222","남길동","053-123-1234","대구 남구");

insert into student(id, pwd, name, phone,address)

    values("3333333","3333","중길동","053-123-1234","대구 중구");

insert into student(id, pwd, name, phone,address)

    values("4444444","4444","서길동","053-123-1234","대구 서구");



#dbtest.php

<?

if($_GET["id"] && $_GET["pwd"]){

$connect = mysql_connect("localhost","root","apmsetup");

mysql_query("set names utf8");

mysql_select_db("mydb",$connect);

$sql = "select * from student where id =".$_GET["id"]." and pwd=".$_GET["pwd"];

$result = mysql_query($sql);

$num = mysql_num_rows($result);


if($num){

$row = mysql_fetch_array($result);

echo "\nwelcome ".$row[name]. "\n";

echo "date:".date("Y-m-d")."\n";

echo "phone :".$row[phone]. "\n";

echo "address :".$row[address]. "\n";

echo "file path:".$_SERVER["PHP_SELF"]." \n";

echo "login ip:".$_SERVER["REMOTE_ADDR"]."\n";

} else {

echo " id or password is not matched";

}

} else {

echo "request page error";

}

?>


http://localhost/dbtest.php?id=4444444&pwd=4444


#MainActivity.java

package com.example.s08;


import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;


import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends Activity {

TextView tv_message;

EditText edt_id, edt_password;

Button btn_cfn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv_message = (TextView)findViewById(R.id.tv_message);

edt_id = (EditText)findViewById(R.id.edt_id);

edt_password = (EditText)findViewById(R.id.edt_password);

btn_cfn = (Button)findViewById(R.id.btn_cfn);

btn_cfn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

if(edt_id.getText().toString().equals("")){

Toast.makeText(MainActivity.this, "아이디를 입력하세요!", Toast.LENGTH_LONG).show();

return;

}

if(edt_password.getText().toString().equals("")) {

Toast.makeText(MainActivity.this, "비밀번호를 입력하세요!", Toast.LENGTH_LONG).show();

return;

}

tv_message.setText("");

String id = edt_id.getText().toString();

String pwd = edt_password.getText().toString();

URL url = null;

HttpURLConnection urlconnection = null;

BufferedReader rbuf = null;

String str = "";

try {

url = new URL("http://220.69.130.192/dbtest.php?id="+id+"&pwd="+pwd); //get방식이기 때문에 url에 접근하여 바로 쏴줌 android->server

urlconnection = (HttpURLConnection)url.openConnection();

if(urlconnection.getResponseCode() == HttpURLConnection.HTTP_OK){

  //HttpURLConnection이 제대로 연결되었으면,

rbuf = new BufferedReader(new InputStreamReader(urlconnection.getInputStream(), "UTF-8"));

while ((str = rbuf.readLine()) != null){

tv_message.append(str+"\n");

}

}

urlconnection.disconnect();

} catch (Exception e) {

Log.i("web","오류 :" + e);

}

}

});

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}


#dbtest.php

<?

if($_GET["id"] && $_GET["pwd"]){

$connect = mysql_connect("localhost","root","apmsetup");

mysql_query("set names utf8");

mysql_select_db("mydb",$connect);

$sql = "select * from student where id =".$_GET["id"]." and pwd=".$_GET["pwd"];

$result = mysql_query($sql);

$num = mysql_num_rows($result); #행의 개수를 반환 1이면 찾음. 0이면 데이터베이스에 데이터가 없음


if($num){

$row = mysql_fetch_array($result); # 쿼리를 통해 반환된 데이터 베이스를 php 관계배열로 변환 row[] 변수 만들어짐

echo "\nwelcome ".$row[name]. "\n";

echo "date:".date("Y-m-d")."\n";

echo "phone :".$row[phone]. "\n";

echo "address :".$row[address]. "\n";

echo "file path:".$_SERVER["PHP_SELF"]." \n";

echo "login ip:".$_SERVER["REMOTE_ADDR"]."\n";

} else {

echo " id or password is not matched";

}

} else {

echo "request page error";

}

?>









'이전것 > 개발' 카테고리의 다른 글

android -> intent  (0) 2016.07.08
android -> Server -> xml  (0) 2016.07.08
android -> webServer  (0) 2016.07.08
android -> webView  (0) 2016.07.07
android -> LifeCycle  (0) 2016.07.07
블로그 이미지

잉비니

,