Add: new php directory, new connect_db.php
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
echo "NA_service_db_setting.php included\n";
|
||||
|
||||
$db_host = "localhost";
|
||||
$db_user = "jisangs";
|
||||
$db_passwd = "Fr12nds95";
|
||||
$db_name = "jisangs";
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
const SERVICE_DB_SETTING_FILE = "./../../server/setup/service_db_setting.php";
|
||||
$has_file = file_exists(SERVICE_DB_SETTING_FILE);
|
||||
if ($has_file) {
|
||||
include(SERVICE_DB_SETTING_FILE);
|
||||
}
|
||||
|
||||
class DBConnector
|
||||
{
|
||||
private $db_conn;
|
||||
private $db_host, $db_user, $db_passwd, $db_name;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
global $db_host, $db_user, $db_passwd, $db_name;
|
||||
|
||||
if (isset($db_host) && $db_host !== null ) {
|
||||
// echo "db_host variable\n";
|
||||
$this->db_host = $db_host;
|
||||
$this->db_user = $db_user;
|
||||
$this->db_passwd = $db_passwd;
|
||||
$this->db_name = $db_name;
|
||||
} else {
|
||||
// echo "no db_host variable\n";
|
||||
$this->db_host = "localhost";
|
||||
$this->db_user = "chocomae";
|
||||
$this->db_passwd = "sEobMPuJ2A8KTfwU";
|
||||
$this->db_name = "chocomae";
|
||||
}
|
||||
$this->printDBVariables();
|
||||
|
||||
$this->connectDB();
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
$this->closeDB();
|
||||
}
|
||||
|
||||
public function connectDB()
|
||||
{
|
||||
// connect db
|
||||
$this->db_conn = new mysqli($this->db_host, $this->db_user, $this->db_passwd, $this->db_name);
|
||||
if ($this->db_conn->connect_error) {
|
||||
// set_error_message("Failed to connect DB: ".$this->db_conn->connect_error);
|
||||
// send_result_fail();
|
||||
echo "connection failed\n";
|
||||
exit;
|
||||
} else {
|
||||
echo "connection succeeded\n";
|
||||
$query = "USE chocomae";
|
||||
$result = mysqli_query($this->db_conn, $query);
|
||||
}
|
||||
}
|
||||
|
||||
public function closeDB()
|
||||
{
|
||||
if ($this->db_conn->connect_error) {
|
||||
$this->db_conn->close();
|
||||
}
|
||||
}
|
||||
|
||||
private function printDBVariables()
|
||||
{
|
||||
echo "host : ".$this->db_host."\n";
|
||||
echo "user : ".$this->db_user."\n";
|
||||
echo "passwd : ".$this->db_passwd."\n";
|
||||
echo "name : ".$this->db_name."\n";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
$jsonReply = array();
|
||||
|
||||
function set_error_message($message) {
|
||||
global $jsonReply;
|
||||
|
||||
$jsonReply["error"] = $message;
|
||||
}
|
||||
|
||||
function set_data($name, $data) {
|
||||
global $jsonReply;
|
||||
|
||||
$jsonReply[$name] = $data;
|
||||
}
|
||||
|
||||
function send_result_success() {
|
||||
global $jsonReply;
|
||||
global $db_conn;
|
||||
|
||||
$jsonReply["result"] = "success";
|
||||
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
|
||||
$db_conn->close();
|
||||
}
|
||||
|
||||
function set_error_code($data) {
|
||||
set_data("error_code", $data);
|
||||
}
|
||||
|
||||
function send_result_fail() {
|
||||
global $jsonReply;
|
||||
global $db_conn;
|
||||
|
||||
$jsonReply["result"] = "fail";
|
||||
echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE);
|
||||
$db_conn->close();
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
include "./../lib/connect_db.php";
|
||||
|
||||
$db_conn = new DBConnector();
|
||||
// $db_conn->setup();
|
||||
|
||||
|
||||
/*
|
||||
|
||||
$maestro_id = $_POST["maestro_id"];
|
||||
$player_id = $_POST["player_id"];
|
||||
|
||||
include "./../lib/send_reply_json.php";
|
||||
include "./../setup/connect_db.php";
|
||||
|
||||
|
||||
$koreanAppList = get_typing_practice_app_list(11);
|
||||
if($koreanAppList === null) {
|
||||
set_error_code("no_app_list");
|
||||
set_error_message("앱 목록을 가져오지 못했습니다.");
|
||||
send_result_fail();
|
||||
exit;
|
||||
}
|
||||
$englishAppList = get_typing_practice_app_list(12);
|
||||
if($englishAppList === null) {
|
||||
set_error_code("no_app_list");
|
||||
set_error_message("앱 목록을 가져오지 못했습니다.");
|
||||
send_result_fail();
|
||||
exit;
|
||||
}
|
||||
|
||||
$koreanActiveAppList = get_active_typing_practice_app_list($maestro_id, 11);
|
||||
$englishActiveAppList = get_active_typing_practice_app_list($maestro_id, 12);
|
||||
|
||||
$koreanHighScoreList = get_high_score_list($maestro_id, $player_id, $koreanAppList);
|
||||
$englishHighScoreList = get_high_score_list($maestro_id, $player_id, $englishAppList);
|
||||
|
||||
set_data("KoreanAppList", $koreanAppList);
|
||||
set_data("EnglishAppList", $englishAppList);
|
||||
set_data("KoreanActiveAppList", $koreanActiveAppList);
|
||||
set_data("EnglishActiveAppList", $englishActiveAppList);
|
||||
set_data("KoreanHighScoreList", $koreanHighScoreList);
|
||||
set_data("EnglishHighScoreList", $englishHighScoreList);
|
||||
send_result_success();
|
||||
exit;
|
||||
|
||||
|
||||
|
||||
function get_typing_practice_app_list($appType) {
|
||||
global $db_conn;
|
||||
|
||||
$query = "
|
||||
SELECT AppID, AppName, KoreanName
|
||||
FROM app
|
||||
WHERE AppType = ? AND Status = 1
|
||||
ORDER BY AppID ASC";
|
||||
$stmt = $db_conn->prepare($query);
|
||||
$stmt->bind_param('i', $appType);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($app_id, $app_name, $korean_name);
|
||||
|
||||
$return_array = array();
|
||||
while($stmt->fetch()) {
|
||||
$row_array['AppID'] = $app_id;
|
||||
$row_array['AppName'] = $app_name;
|
||||
$row_array['KoreanName'] = $korean_name;
|
||||
array_push($return_array, $row_array);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
return $return_array;
|
||||
}
|
||||
|
||||
function get_active_typing_practice_app_list($maestro_id, $appType) {
|
||||
global $db_conn;
|
||||
|
||||
$query = "
|
||||
SELECT A.AppID, A.AppName, A.KoreanName
|
||||
FROM app AS A
|
||||
INNER JOIN active_app AS AA
|
||||
ON A.AppID = AA.AppID AND AA.MaestroID = ? AND A.AppType = ?
|
||||
ORDER BY A.AppID ASC";
|
||||
$stmt = $db_conn->prepare($query);
|
||||
$stmt->bind_param('ii', $maestro_id, $appType);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($app_id, $app_name, $korean_name);
|
||||
|
||||
$return_array = array();
|
||||
while($stmt->fetch()) {
|
||||
$row_array['AppID'] = $app_id;
|
||||
$row_array['AppName'] = $app_name;
|
||||
$row_array['KoreanName'] = $korean_name;
|
||||
array_push($return_array, $row_array);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
return $return_array;
|
||||
}
|
||||
|
||||
function get_high_score_list($maestro_id, $player_id, $appList) {
|
||||
global $db_conn;
|
||||
|
||||
$return_array = array();
|
||||
$count = count($appList);
|
||||
|
||||
for($i = 0; $i < $count; $i++) {
|
||||
$app_id_list = $appList[$i]['AppID'];
|
||||
|
||||
$query = "
|
||||
SELECT MAX(AHR.HighestRecord)
|
||||
FROM app AS A
|
||||
INNER JOIN app_highest_record AS AHR
|
||||
ON A.AppID = ? AND A.AppID = AHR.AppID AND AHR.MaestroID = ? AND AHR.PlayerID = ?
|
||||
ORDER BY A.AppID ASC";
|
||||
$stmt = $db_conn->prepare($query);
|
||||
$stmt->bind_param('iii', $app_id_list, $maestro_id, $player_id);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($app_highest_record);
|
||||
|
||||
$returnValue = $stmt->fetch();
|
||||
$stmt->close();
|
||||
|
||||
if($returnValue === NULL || $returnValue === FALSE)
|
||||
continue;
|
||||
|
||||
// $row_array['maestro_id'] = $maestro_id;
|
||||
// $row_array['player_id'] = $player_id;
|
||||
$row_array['AppID'] = $app_id_list;
|
||||
if($app_highest_record === NULL)
|
||||
$row_array['AppHighestRecord'] = 0;
|
||||
else
|
||||
$row_array['AppHighestRecord'] = $app_highest_record;
|
||||
|
||||
array_push($return_array, $row_array);
|
||||
}
|
||||
|
||||
return $return_array;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user