/** * * The createMethod creates the Database which are listed in Config if they doesn't exist or replace the existing if the user accept * * @return void */ public static function create() { //init Database $db = new db(); Config::dbConfig(); //check all listed Databases foreach (Config::dbConfig()['databases'] as $key => $value) { //if database already exist ask if the user want to replace it, else create the Database $sqlCheck = "SHOW DATABASES LIKE '{$value}'"; $result = $db::$db->query($sqlCheck); if (!mysqli_num_rows($result) > 0) { //create database $sql = "CREATE DATABASE {$value}"; $db::$db->query($sql) or die("It went something wrong by creating Database\n"); } else { //replace the existing Databases if the user press 'y' echo "The Database '{$value}' does already exist do you want to replace it\npress y for yes and another for no:"; $answer = trim(fgets(STDIN)); if ($answer === 'y') { //drop existing $sql = "DROP DATABASE IF EXISTS {$value}"; $db::$db->query($sql) or die("It went something wrong by droping Database maybe you don't have rights \n"); //create database $sql = "CREATE DATABASE {$value}"; $db::$db->query($sql) or die($db->error); } } } }
/** * * The Constructor sets the dbConfigs and inits the DataBase * * @return void */ public function __construct() { //set probertyVariables $host = Config::dbConfig()['host']; $username = Config::dbConfig()['username']; $password = Config::dbConfig()['password']; $databases = Config::dbConfig()['databases']; //init the DataBase self::init($host, $username, $password, $databases); }