Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MySql Help

Comments in 'Plugin Development' started by Tethered_, Jun 30, 2015.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    The logger shows that I have connected to the database, but I cannot query to it correctly or add tables because I get "Master Table Failed" then "Player is not in database" then "Could not add entry" and then "Player is not in database" when I join the game so it's clearly an error on my part with MySQL. If someone could point me in the right direction that would be great!

    PHP:
    <?php

    namespace gladiusdata;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\level\particle\FloatingTextParticle;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\utils\TextFormat;
    use 
    gladiusinfo\AnnounceTask;
    use 
    gladiusinfo\gladiusinfo;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;

    class 
    Main extends PluginBase implements Listener
    {
        private 
    $db;
      
        public function 
    onEnable()
        {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
          
            
    $user /*username*/;
            
    $pass /*password*/;
            
    $server /*server*/;
            
    $database /*database*/;
            
    $port /*port*/;
          
            
    $this->db = @mysqli_connect($server$user$pass$database$port);
            if(!
    $this->db)
            {
                
    $this->getServer()->getLogger()->info(TextFormat::RED "Did not work");
            } else {
                
    $this->getServer()->getLogger()->info(TextFormat::GREEN "Worked!");
            }
          
            if(
    $this->db->query("CREATE TABLE IF NOT EXISTS master (player TEXT PRIMARY KEY COLLATE NOCASE, rank TEXT, bal INTEGER);"))
            {
                
    $this->getServer()->getLogger()->info(TextFormat::GREEN "Created master table");
            } else {
                
    $this->getServer()->getLogger()->info(TextFormat::RED "Master table failed");
            }
        }
      
        public function 
    onPlayerJoin(PlayerJoinEvent $pje)
        {
            
    $this->insert($pje->getPlayer()->getName());
        }
      
        public function 
    insert($player$rank "none"$bal 0)
        {
            if(!
    $this->playerExists($player))
            {;
                
    $sql "INSERT INTO master (player, rank, bal) VALUES ($player$rank$bal)";
                if(
    $this->db->query($sql))
                {
                    
    $this->getServer()->getLogger()->info(TextFormat::GREEN "Added new entry successfully");
                } else {
                    
    $this->getServer()->getLogger()->info(TextFormat::RED "Could not add entry");
                }
            }
            
    $this->playerExists($player);
        }
      
        public function 
    playerExists($player)
        {
            
    $result $this->db->query("SELECT * FROM master WHERE player='$player'");
            if(
    $result)
            {
                
    $this->getServer()->getLogger()->info(TextFormat::GREEN "Player is in database!");
            } else {
                
    $this->getServer()->getLogger()->info(TextFormat::RED "Player is not in database!");
            }
        }
    }
    Last edited: Jun 30, 2015
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You have to change $player into player name, and use the MySQLi escape method and put quotes around it.
    Lambo likes this.

Share This Page

Advertisement