diff --git a/src/web/php/lib/connect_db.php b/src/web/php/lib/connect_db.php index aa2ccdd..1acf559 100644 --- a/src/web/php/lib/connect_db.php +++ b/src/web/php/lib/connect_db.php @@ -15,7 +15,7 @@ class DBConnector { global $db_host, $db_user, $db_passwd, $db_name; - if (isset($db_host) && $db_host !== null ) { + if (isset($db_host) && $db_host !== null) { // echo "db_host variable\n"; $this->db_host = $db_host; $this->db_user = $db_user; @@ -28,14 +28,10 @@ class DBConnector $this->db_passwd = "sEobMPuJ2A8KTfwU"; $this->db_name = "chocomae"; } - $this->printDBVariables(); - - $this->connectDB(); } function __destruct() { - $this->closeDB(); } public function connectDB() @@ -43,30 +39,36 @@ class DBConnector // 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); + return false; } + + $query = "USE chocomae"; + $result = mysqli_query($this->db_conn, $query); + if (!$result) + return false; + + return true; } public function closeDB() { + if (isset($this->db_conn) || $this->db_conn === null) { + return; + } + if ($this->db_conn->connect_error) { $this->db_conn->close(); } } - private function printDBVariables() + public function getDBVariables() { - echo "host : ".$this->db_host."\n"; - echo "user : ".$this->db_user."\n"; - echo "passwd : ".$this->db_passwd."\n"; - echo "name : ".$this->db_name."\n"; + $variables = array(); + $variables["host"] = $this->db_host; + $variables["user"] = $this->db_user; + $variables["passwd"] = $this->db_passwd; + $variables["name"] = $this->db_name; + return $variables; } } diff --git a/src/web/php/writing/player_list.php b/src/web/php/writing/player_list.php index 1dc928a..dd527a2 100644 --- a/src/web/php/writing/player_list.php +++ b/src/web/php/writing/player_list.php @@ -3,8 +3,15 @@ header('Content-Type: application/json'); include "./../lib/connect_db.php"; + +$jsonReply = array(); + $db_conn = new DBConnector(); -// $db_conn->setup(); +$isConnected = $db_conn->connectDB(); + +$jsonReply["isConnected"] = $isConnected; +$jsonReply["dbVariables"] = $db_conn->getDBVariables(); +echo json_encode($jsonReply, JSON_UNESCAPED_UNICODE); /* diff --git a/test/tdd.js b/test/tdd.js index 1d60b34..50af472 100644 --- a/test/tdd.js +++ b/test/tdd.js @@ -1,3 +1,23 @@ +////////////////////////////////////////////////// +// DBConnector + +QUnit.test( "DBConnector", function( assert ) { + + var xhr = new XMLHttpRequest(); + xhr.open("POST", "./../src/web/php/writing/player_list.php", true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.onreadystatechange = function() { + if(xhr.readyState == 4 && xhr.status == 200) { + var replyJSON = JSON.parse(xhr.responseText); + console.log(replyJSON); + } + }; + xhr.send(); + + assert.equal(1, 0, "DBConnector"); +}); + + ////////////////////////////////////////////////// // StringUtil