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

Solved MySQL

Comments in 'Plugin Development' started by CraftYourBukkit, Nov 7, 2015.

  1. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    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:
    <?php

    namespace 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() {
           
        }

    }
  2. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    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 :D
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    There's no mysqli::execute()
  4. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Oops, right. Only after ->prepare
    Change your if connect error to if connect error > 0
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Why? It does connect, it sends the message. But it doesn't create the Table
  6. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    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 ']');
    }
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Thanks!

Share This Page

Advertisement