Advertisement
TungstenVn

mysql

Aug 4th, 2021
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace tungsten3;
  4.  
  5. use pocketmine\plugin\PluginBase;
  6. use pocketmine\Server;
  7. use pocketmine\event\server\QueryRegenerateEvent;
  8. class connectMysql{
  9.    
  10.  
  11.     /** @var static string , dont change */
  12.     public $db;
  13.     public $host = "137.116.130.254";
  14.     public $user = "root";
  15.     public $password = "";
  16.     public $dbname = "onlinedb";
  17.     public $query;
  18.     public $pl;
  19.    
  20.     public function __construct(Main $pl){
  21.        $this->pl = $pl;
  22.        $this->connectMysqlwithDB();
  23.     }
  24.    
  25.     public function connectMysqlwithDB(){
  26.         $this->db = @new \mysqli($this->host, $this->user, $this->password,$this->dbname);
  27.         if ($this->db->connect_error) {
  28.             $this->pl->getLogger()->info("\n\n              §aDont have database, Going to create one....\n");
  29.            $this->connectMysqlwithoutDB();
  30.         }else{
  31.             $this->pl->getLogger()->info("\n\n               §aMultiChat enabled\n");
  32.             $this->createTB();     
  33.         }
  34.     }
  35.     public function connectMysqlwithoutDB(){
  36.         $this->db = @new \mysqli($this->host, $this->user, $this->password,3306);
  37.         if (!$this->db->connect_error) {
  38.            $this->createDB();
  39.         }else{
  40.             $this->pl->getLogger()->info("\n             §cYou didnt enable a mysql service,so the plugin cannot be enable...\n");
  41.             $this->pl->getServer()->getPluginManager()->disablePlugin($this->pl);
  42.         }
  43.        
  44.     }
  45.     public function createDB(){
  46.         if (!$this->db->query("CREATE DATABASE IF NOT EXISTS ".$this->dbname)){
  47.             $this->pl->getLogger()->info("Failed on creating: " . $this->db->error);
  48.         }else{
  49.         $this->connectMysqlwithDB();
  50.         }
  51.     }
  52.    
  53.     public function createTB(){
  54.         if (!$this->db->query("CREATE TABLE IF NOT EXISTS ".$this->pl->thisserver." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY  KEY,server TEXT,name TEXT,message TEXT);")){
  55.             $this->getLogger()->info("Failed on creating table: " . $this->db->error);
  56.          }
  57.         for($i = 0;$i<$this->pl->numberofserver -1;$i++){
  58.          if (!$this->db->query("CREATE TABLE IF NOT EXISTS ".$this->pl->server[$i]." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,server TEXT,name TEXT,message TEXT);")){
  59.             $this->getLogger()->info("Failed on creating table: " . $this->db->error);
  60.          }
  61.         }
  62.     }
  63.    
  64.    
  65.    
  66.    
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement