๐ก PHP๋ฅผ ์ด์ฉํ์ฌ ์ธ๋ถ DB์ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ!
- ์๋๋ก์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด์์์ ์ด์ ๋ก ‘์ธ๋ถ’ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ๋ก ์ ๊ทผํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ (php+์๋ฒ)๋ฅผ ์ค๊ฐ์ ๋๊ณ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉํด์ผ ํจ
๐ก ์ฝ๋ ์์ฑ
- ๋จผ์ build.gradle(App)ํ์ผ์ ์๋ฒ ํต์ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ implementation 'com.android.volley:volley:1.2.1' ์ ์ถ๊ฐํด์ค์ผ ํจ
Activity์์ Response & Request ๋ฉ์๋ ์์ฑ
- Response Listener๋ฅผ ์์ฑํ๊ณ onResponse()๋ฉ์๋ @Overrideํ๋ค.
- JSONArray or JSONObject๊ฐ์ฒด๋ก ๊ฐ์ ๋ฐํ ๋ฐ์์ ์ฌ์ฉํ๋ค.
- ๋ง์ง๋ง์๋ RequestActivity๊ฐ์ฒด์ ์ ๋ฌํ ๊ฐ์ ๋ด์ ์์ฑํ๊ณ RequestQueue๊ฐ์ฒด์ ๋ด๋๋ค.
private void donation(String category) {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// ๋ถ๋ฌ์ค๊ธฐ ์ ์ ๋ฐ์ดํฐ(์์ดํ
) ์ด๊ธฐํ ํด์ค์ผ ์ค์ฒฉ ์๋จ.
adapter.notifyDataSetChanged();
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = (JSONObject) jsonArray.getJSONObject(i);
String titleName = jsonObject.getString("titleName");
String name = jsonObject.getString("name");
String nowStep = jsonObject.getString("nowStep");
String content = jsonObject.getString("content");
String date = jsonObject.getString("date");
String startDate = jsonObject.getString("startDate");
String maxStep = jsonObject.getString("maxStep");
String dNum = jsonObject.getString("dNum");
String contentImage = jsonObject.getString("contentImage");
DonationData mainData = new DonationData(dNum, titleName, name, nowStep, contentImage, content, date, startDate, maxStep);
arrayList.add(mainData);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
DonationRequest donationRequest = new DonationRequest(category, responseListener);
RequestQueue queue = Volley.newRequestQueue(getActivity());
queue.add(donationRequest);
}
์๋ฒ๋ก ์์ฒญํ Request ํ์ผ ์์ฑ
- ์๋ฒ URL์ค์ (phpํ์ผ ์ฐ๋)
- Requestํด๋์ค์ ์์ฑ์ ์์ฑ
- ํธ์ถํ ๋ ๋ฐ์์ฌ ๋งค๊ฐ๋ณ์ ์ง์
- super()์ ์ ์ก๋ฐฉ์(POST), listener, errorListener์ง์
- Mapํ์ ๊ฐ์ฒด์ ์๋ฒ๋ก ์ ์กํ ๊ฐ ๋ด๊ธฐ
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
// Donation๊ฐ ์๋ฒ๋ก ์ ์ก (2023-03-18 ์ฐ์ง ์์ )
public class DonationInsertRequest extends StringRequest {
final static private String URL = "<http://miraclestep01.dothome.co.kr/InsertDonation.php>"; // ์๋ฒ URL ์ค์ (PHP ํ์ผ ์ฐ๋.)
private Map<string, string=""> map;
public DonationInsertRequest(int dNum, String userId, String userStep, Response.Listener listener) {
// post๋ฐฉ์์ผ๋ก listener๋ฅผ ์๋ฒ์ ์ ์ก.
super(Request.Method.POST, URL, listener, null);
// ํ์ฌ ๋ ์ง ๊ตฌํ๊ธฐ
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatedNow = now.format(formatter);
map = new HashMap<>();
map.put("dNum", String.valueOf(dNum));
map.put("userID", userId);
map.put("userStep", userStep);
map.put("ddate", String.valueOf(formatedNow));
Log.d("์ด๋", "์ธ์ํธ");
Log.d("๊ธ๋ฒํธ", String.valueOf(dNum));
Log.d("์์ด๋", userId);
Log.d("๊ธฐ๋ถ๋ ์ง", formatedNow);
}
@Override
protected Map<string, string=""> getParams() throws AuthFailureError {
return map;
}
}
</string,></string,>
์๋ฒ์ ๋ฑ๋กํ php ํ์ผ ์์ฑ
- ์๋ฒ DB์ ์ฐ๊ฒฐ - mysqli_connect()
- ip์ฃผ์, id, pw, database, ์ธ์ฝ๋ฉ ์ค์
- Requestํ์ผ์์ Map๊ฐ์ฒด์ ๋ด์ ๋ณด๋ธ ๊ฐ ์ธํ
- Sql๋ฌธ ์์ฑ ๋ฐ ์คํ
- mysqli_prepare(), mysqli_stmt_execute()
- ๊ฒฐ๊ณผ ๊ฐ ๋ฐ์์ค๊ธฐ ๋ฐ ๊ฒฐ๊ณผ ์ ์ก
select
<?php
/*
SelectUserDonation.php : ๊ธฐ๋ถ๋ด์ญ์ ๋ถ๋ฌ์ค๋ php (POST ํ์์ผ๋ก MySQL๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ด.)
con : mysql ์ฐ๊ฒฐ์ ์๋ํ๋ ๋ณ์.
"$๋ณ์" ๋ก ๋ณ์ ์ ์ธ
*/
/* (1) ์๋ฒ DB์ ์ฐ๊ฒฐ.*/
$con = mysqli_connect("localhost", "miraclestep01", "๋น๋ฐ๋ฒํธ", "miraclestep01"); // mysql ์ฐ๊ฒฐ, IP, ์ฌ์ฉ์๋ช
, ๋น๋ฐ๋ฒํธ, ๋ฐ์ดํฐ๋ฒ ์ด์ค
mysqli_query($con, 'SET NAMES utf8'); // ์ธ์ฝ๋ฉ์ utf-8๋ก ์ธํ
. (ํ๊ธ ์ ์ก์ด ๊ฐ๋ฅํด์ง.)
$userID = isset($_POST["userID"]) ? $_POST["userID"] : "test1234@naver.com";
$statement = mysqli_prepare($con, "SELECT CampaignList.hostingGroup, CampaignList.title , DonationHistory.donationDate, DonationHistory.donationStep FROM CampaignList LEFT JOIN DonationHistory ON CampaignList.campaignIndex = DonationHistory.donationIndex WHERE DonationHistory.userId = ? ORDER BY DonationHistory.donationDate DESC");
mysqli_stmt_bind_param($statement, "s", $userID);
mysqli_stmt_execute($statement);
//mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $DonationGroup, $DonationName, $UserDonationDate, $UserDonationStep);
/*$statement2 = mysqli_prepare($con, "SELECT DGroup, DName FROM Donation WHERE DNum = ".$DonationNumber);
//mysqli_stmt_bind_param($statement2, "i", $DonationNumber);
mysqli_stmt_execute($statement2);
mysqli_stmt_store_result($statement2);
mysqli_stmt_bind_result($statement2, $DonationGroup, $DonationName);*/
$response = array();
$allResponse = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["donationDate"] = $UserDonationDate;
$response["donationStep"] = $UserDonationStep;
$response["titleName"] = $DonationName;
$response["groupName"] = $DonationGroup;
$allResponse[] = $response;
}
echo json_encode($allResponse);
?>
insert
<?php
/*
InsertDonation.php : ๊ฑธ์ ์๋ฅผ ๊ธฐ๋ถํ๋ ์ฌ์ฉ์ php (POST ํ์์ผ๋ก MySQL๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ด.)
con : mysql ์ฐ๊ฒฐ์ ์๋ํ๋ ๋ณ์.
"$๋ณ์" ๋ก ๋ณ์ ์ ์ธ
*/
/* (1) ์๋ฒ DB์ ์ฐ๊ฒฐ.*/
$con = mysqli_connect("localhost", "miraclestep01", "๋น๋ฐ๋ฒํธ", "miraclestep01");
mysqli_query($con, 'SET NAMES utf8'); /* ์ธ์ฝ๋ฉ์ utf-8๋ก ์ธํ
. (ํ๊ธ ์ ์ก์ด ๊ฐ๋ฅํด์ง.) */
/* (2) DB์ ์ ์ฅํ ๊ฐ์ฒด ์ ์ธ. */
$userID = isset($_POST["userID"]) ? $_POST["userID"] : "";
// dateํ์
์ php๋ String์ผ๋ก ๋ฐ์๋ค์ด๊ธฐ ๋๋ฌธ์ ํ๋ณํ
$ddate = isset($_POST["ddate"]) ? $_POST["ddate"] : "";
$ddate=str_replace(".","-",$ddate);
$ddate=str_replace("/","-",$ddate);
$ddate = date('Ymd', strtotime($ddate));
$dNum = isset($_POST["dNum"]) ? $_POST["dNum"] : "";
$userStep = isset($_POST["userStep"]) ? $_POST["userStep"] : "";
/* (3) DB์์ insertํ๋ ๋ฌธ์ฅ. (UserDonation) */
$statement = mysqli_prepare($con, "INSERT INTO DonationHistory (donationIndex, userId, donationDate, donationStep) VALUES (?,?,?,?)");
mysqli_stmt_bind_param($statement, "issi", $dNum, $userID, $ddate, $userStep);
mysqli_stmt_execute($statement);
/* (4) ์ฑ๊ณต ์ฌ๋ถ ์ ์ก. */
$response = array();
$response["success"] = true;
/* (5) ์คํ ๊ฒฐ๊ณผ ์ ์ก. */
echo json_encode($response);
?>
update
<?php
/*
UpdateDonation.php : ํด๋น ์บ ํ์ธ์ ํ์ฌ ๊ธฐ๋ถ๋ ๊ฑธ์ ์๋ฅผ ์ฒ๋ฆฌํ๋ php (POST ํ์์ผ๋ก MySQL๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ด.)
con : mysql ์ฐ๊ฒฐ์ ์๋ํ๋ ๋ณ์.
"$๋ณ์" ๋ก ๋ณ์ ์ ์ธ
*/
/* (1) ์๋ฒ DB์ ์ฐ๊ฒฐ.*/
$con = mysqli_connect("localhost", "miraclestep01", "๋น๋ฐ๋ฒํธ", "miraclestep01");
mysqli_query($con, 'SET NAMES utf8'); /* ์ธ์ฝ๋ฉ์ utf-8๋ก ์ธํ
. (ํ๊ธ ์ ์ก์ด ๊ฐ๋ฅํด์ง.) */
/* (2) DB์ ์ ์ฅํ ๊ฐ์ฒด ์ ์ธ. */
$dNum = isset($_POST["dNum"]) ? $_POST["dNum"] : "0";
$updateStep = isset($_POST["updateStep"]) ? $_POST["updateStep"] : "";
/* (3) DB์์ insertํ๋ ๋ฌธ์ฅ. (Donation) */
$statement = mysqli_prepare($con, "UPDATE CampaignList SET nowDonation = ? Where campaignIndex = ?");
mysqli_stmt_bind_param($statement, "ii", $updateStep, $dNum);
mysqli_stmt_execute($statement);
/* (4) ์ฑ๊ณต ์ฌ๋ถ ์ ์ก. */
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
'Programming Language > PHP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[PHP] PHP ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2024.06.13 |
---|---|
[PHP] PHP (0) | 2024.06.13 |