I am new to MySQL and tried it a bit. Well, my code doesn't create a Table but connects to the Database. Do you know why? PHP: <?phpnamespace BukkitPlaysMC\BanSystem;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\event\player\PlayerChatEvent;use pocketmine\utils\Config;class Main extends PluginBase implements Listener { public $database; public function onEnable() { if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); $c = new Config($this->getDataFolder() . "config.yml", Config::YAML); $c->save(); $config = $c->get("MySQL"); $this->database = mysqli_connect($config["host"], $config["user"], $config["password"], $config["database"], isset($config["port"]) ? $config["port"] : 3306); if ($this->database->connect_error) { $this->plugin->getLogger()->critical("MYSQL ERROR - CAN'T CONNECT TO MYSQL: " . $this->database->connect_error); } $this->getLogger()->info("Connected to MySQL server"); $mysql = "CREATE TABLE IF NOT EXISTS bans ( name VARCHAR(20) PRIMARY KEY, by VARCHAR(20), reason VARCHAR(255), date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, ip VARCHAR(15), id CHAR(255), duration INT DEFAULT 0 )"; $this->database->query($mysql); } // Just test! public function onChat(PlayerChatEvent $event) { $player = $event->getPlayer(); $message = $event->getMessage(); $name = $player->getName(); $ip = $player->getAddress(); $id = $player->getUniqueId(); $mysql = "INSERT INTO bans VALUES('$name', 'Test', '$message', time(), $ip, $id)"; $this->database->query($mysql); } public function onDisable() { }}
You do not execute the query. (I also have troubles with mysql) $this->database->execute(); Atleast i think --=+=-- AND YOU ARE ONE MORE PERSON BANNING OTHERS ON CHAT OH MY GOT WEIRD PLAYER HANDLING INCOMING xD
PHP: $this->database = mysqli_connect($config["host"], $config["user"], $config["password"], $config["database"], isset($config["port"]) ? $config["port"] : 3306);if ($this->database->connect_error()) {$this->plugin->getLogger()->critical("MYSQL ERROR - CAN'T CONNECT TO MYSQL: " . $this->database->connect_error);}$this->getLogger()->info("Connected to MySQL server");$mysql = "CREATE TABLE IF NOT EXISTS bans (`name` VARCHAR(20) PRIMARY KEY,`by` VARCHAR(20),`reason` VARCHAR(255),`date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,`ip` VARCHAR(15),`id` CHAR(255),`duration` INT DEFAULT 0)";if(!$result = $this->database->query($sql)){ $this->plugin->getLogger()->critical('There was an error running the query [' . $db->error . ']');}