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 $sender, Command $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.
The command doesn't work, And in rare instances when joining the setRank function doesn't work properly.
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.
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."'"; }
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); }