Add: new php directory, new connect_db.php

This commit is contained in:
2019-07-10 08:06:45 +09:00
parent 1715c557e7
commit b25b661032
4 changed files with 266 additions and 0 deletions
+73
View File
@@ -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";
}
}
?>