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

How Can I Fix This?

Comments in 'Plugin Development' started by Calrizer, Jul 23, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Hi,
    I've been reading up on the relation between MySQL and PHP and I have attempted to add it to my plugin. It was working to an extent but now by adding things it has stopped working properly. Heres the the code see if you can point out anything obviously wrong.
    PHP:
     public function onEnable(){
           
            
    //SQL Connect
           
            
    $server "dbhostname";
           
            
    $port 3306;
           
            
    $database "db1";
           
            
    $username "username";
           
            
    $password "password";
           
            
    $this->connect = new \mysqli($server$username$password$database$port);
           
            if(
    $this->connect->connect_error){
                
    $this->getLogger()->info("Connection Error: "$this->connect->connect_error);
            }else{
                
    $this->getLogger()->info("Connected To SQL Server!");
            }
           
            
    $table "CREATE TABLE Ranks (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, rank VARCHAR(30) NOT NULL)";
           
            if(
    $this->connect->query($table) === true){
                
    $this->getLogger()->info("Ranks Table Created!");
            }else{
                
    $this->getLogger()->info("Error: ".$this->connect->connect_error);
            }

    }

    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
           
            switch(
    $command->getName()){
    case 
    "setrank":
    if(
    count($args) !== 2){
    $sender->sendMessage(TextFormat::RED "Usage: " $command->getUsage());
    return 
    true;
    }

                    
    $player $args[0];
                   
    $rank $args[1];

    $this->updateRank($player$rank);
               
    $sender->sendMessage("Rank set correctly!");

                    return 
    true;
         

    public function 
    onJoin(PlayerJoinEvent $event){
           
            
    $player $event->getPlayer();
           
            if(
    $this->getPlayedBefore($player) === false){
                
    $this->setRank($player"Player");
            }
    }

    //Update Rank Function
       
        
    public function updateRank($player$rank){
           
            
    $update "UPDATE Ranks SET rank='".$rank."' WHERE name='".$player."'";
           
    }

    //Get Rank Function
       
        
    public function getRank($player){

           
        
    $player = (string) $player->getName();
           
            
    $get "SELECT name, rank FROM Ranks";
           
            
    $result $this->connect->query($get);
           
            if(
    $result->num_rows 0){
                while(
    $row $result->fetch_assoc()){
                    if(
    $row["name"] = $player){
                        
    $rank $row[“rank”];
                    }
                }
            }
           
            return 
    $rank;
           
    }
       
        public function 
    getPlayedBefore($player){

           
        
    $player $player->getName();
           
            
    $get "SELECT name FROM Ranks";
           
            
    $result $this->connect->query($get);
           
            if(
    $result->num_rows 0){
                while(
    $row $result->fetch_assoc()){
                    if(
    $row["name"] = $player){
                        return 
    true;
                    }else{
                        return 
    false;
                    }
                }
            }
    }
    Any feedback is appreciated.
  2. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    BTW the table is only created once and ignore the pasting errors
    Last edited: Jul 23, 2015
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    And where is the problem?
  4. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    The command doesn't work,
    And in rare instances when joining the setRank function doesn't work properly.
  5. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    What is doing function updateRank()?
  6. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    It accesses the Ranks table on the SQL server and finds where the player given in the command arguments and changes the rank to the second arguement.
  7. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    Im maybe stupid but i dont see any output xD
    PHP:
    public function updateRank($player$rank){
     
    $update "UPDATE Ranks SET rank='".$rank."' WHERE name='".$player."'";
     
    }
  8. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    You are not stupid good sir, I have Update Rank working now by adding this:
    You are not stupid good sir, I have Update Rank working now by adding this:
    PHP:
    if($this->connect->query($update) === true){
                
    $this->getLogger()->info("Rank Updated For: ".$player);
           
            }else{
                
    $this->getLogger()->info("Error Updating Rank: ".$this->connect->error); 
            }

Share This Page

Advertisement