1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

I need help to connect plugin to mysql

Discussion in 'Development' started by abdullah, Jun 23, 2018.

  1. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    hello pmmp community ,

    I use this code to connect mysql database in my plugin
    PHP:
    <?php

    namespace ReportSpace;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\plugin\Plugin as Plugin;
    use 
    pocketmine\plugin\PluginDescription;
    use 
    pocketmine\plugin\PluginLoader;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat as T;
    use 
    pocketmine\player;

    class 
    Main extends PluginBase
    {

        public 
    $HOST;
        public 
    $USER;
        public 
    $PASS;
        public 
    $DB;
        public 
    $CON;

        public function 
    CON()
        {

            
    $this->HOST "localhost";
            
    $this->USER "root";
            
    $this->PASS "";
            
    $this->DB "minespace";

            
    $con = new \mysqli($this->HOST$this->USER$this->PASS);
            if (
    $con->connect_error)
            {

                
    $this->getServer()->shutdown();

            }

            
    $con->query("CREATE DATABASE IF NOT EXISTS $this->DB CHARACTER SET utf8 COLLATE utf8_general_ci");

            
    $this->CON = new \mysqli($this->HOST$this->USER$this->PASS$this->DB);
            if (
    $this->CON->connect_error)
            {

                
    $this->getServer()->shutdown();

            }
            
    $this->CON->query("CREATE TABLE IF NOT EXISTS report ( player varchar(255) , hacker varchar(255) , problem varchar(255) )");
            
    $this->CON->query("CREATE TABLE IF NOT EXISTS sugg ( player varchar(255) , sugg varchar(255) )");
            
    $this->CON->query("CREATE TABLE IF NOT EXISTS inst ( player varchar(255) , url varchar(255), PRIMARY KEY (url) )");
            
    $this->CON->query("CREATE TABLE IF NOT EXISTS yout ( player varchar(255) , url varchar(255), PRIMARY KEY (url) )");

            return 
    $this->CON;

        }

        public function 
    onEnable()
        {

            
    $this->getLogger()->info(T::DARK_GREEN "ReportSpace Created by s_ki OR ski12");

        }
        public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args): bool
        
    {

            switch (
    $cmd->getName())
            {

                case 
    'contact':
                    
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
                    if (
    $api === null || $api->isDisabled()){return false;}
                    
    $form $api->createSimpleForm(function (Player $playerint $data null){

                        
    $result $data;
                        if (
    $result === null){return false;}

                        switch (
    $result)
                        {

                            case 
    0:
                                
    $this->report($player);
                                break;

                            case 
    1:
                                
    $this->sugg($player);
                                break;

                            case 
    2:
                                
    $this->inst($player);
                                break;

                            case 
    3:
                                
    $this->yout($player);

                        }

                    });

                    
    $form->setTitle(T::GOLD "MineSpace");
                    
    $form->addButton(T::DARK_PURPLE "Report a Hacker"0);
                    
    $form->addButton(T::DARK_PURPLE "Complaints and Suggestions"0);
                    
    $form->addButton(T::DARK_PURPLE "Instagrammer Rank Request"0);
                    
    $form->addButton(T::DARK_PURPLE "Youtuber Rank Request"0);
                    
    $form->sendToPlayer($sender);

                    return 
    true;

            }


        }
        public function 
    report(Player $player)
        {

            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            if (
    $api === null || $api->isDisabled()){return false;}

            
    $n $player->getName();

            
    $form $api->createCustomForm(function (Player $player, array $data){

                
    $result $data[0];
                if (
    $result != null){return false;}

                
    $n $player->getName();

                switch (
    $result)
                {

                    case 
    0:
                        
    $this->CON()->query("INSERT INTO `report`(`PLAYER`, `HACKER`, `PROBLEM`) VALUES ($n$data[1]$data[2])");
                        
    $player->sendMessage(T::GREEN "your Report was successfully sent");
                        break;

                }

            });
            
    $form->setTitle(T::GOLD "MineSpace");
            
    $form->addLabel(T::GREEN "Report a Hacker");
            
    $form->addInput("Hacker Name""eg : $n""");
            
    $form->addInput("problem""eg : fly in the game""");
            
    $form->sendToPlayer($player);

            return 
    true;

        }
        public function 
    sugg(Player $player)
        {

            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            if (
    $api === null || $api->isDisabled()){return false;}

            
    $n $player->getName();

            
    $form $api->createCustomForm(function (Player $player, array $data){

                
    $result $data[0];
                if (
    $result != null){return false;}

                
    $n $player->getName();

                switch (
    $result)
                {

                    case 
    0:
                        
    $this->CON()->query("INSERT INTO `sugg`(`PLAYER`, `sugg`) VALUES ($n$data[1])");
                        
    $player->sendMessage(T::GREEN "your Complaints or Suggestions was successfully sent");
                        break;

                }

            });
            
    $form->setTitle(T::GOLD "MineSpace");
            
    $form->addLabel(T::GREEN "Complaints and Suggestions");
            
    $form->addInput("message""eg : x o game disabled""");
            
    $form->sendToPlayer($player);

            return 
    true;

        }
        public function 
    inst(Player $player)
        {

            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            if (
    $api === null || $api->isDisabled()){return false;}

            
    $n $player->getName();

            
    $form $api->createCustomForm(function (Player $player, array $data){

                
    $result $data[0];
                if (
    $result != null){return false;}

                
    $n $player->getName();

                switch (
    $result)
                {

                    case 
    0:
                        
    $this->CON()->query("INSERT INTO `inst`(`PLAYER`, `url`) VALUES ($n$data[1])");
                        
    $player->sendMessage(T::GREEN "your Request was successfully sent");
                        break;

                }

            });
            
    $form->setTitle(T::GOLD "MineSpace");
            
    $form->addLabel(T::GREEN "Instagrammer Rank Request");
            
    $form->addLabel(T::GREEN "شروط الانستقرامر هنا يا كفكف");
            
    $form->addInput("your instagram post URL""eg : https://www.instagram.com/p/BgD7WcPhgoE/""");
            
    $form->sendToPlayer($player);

            return 
    true;

        }

        public function 
    yout(Player $player)
        {

            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            if (
    $api === null || $api->isDisabled()){return false;}

            
    $n $player->getName();

            
    $form $api->createCustomForm(function (Player $player, array $data){

                
    $result $data[0];
                if (
    $result != null){return false;}

                
    $n $player->getName();

                switch (
    $result)
                {

                    case 
    0:
                        
    $this->CON()->query("INSERT INTO `yout`(`PLAYER`, `url`) VALUES ($n$data[1])");
                        
    $player->sendMessage(T::GREEN "your Request was successfully sent");
                        break;

                }

            });
            
    $form->setTitle(T::GOLD "MineSpace");
            
    $form->addLabel(T::GREEN "Youtuber Rank Request");
            
    $form->addLabel(T::GREEN "شروط اليوتيوبر هنا يا كفكف");
            
    $form->addInput("your youtube video URL""eg : https://youtu.be/L_FmO3WtG6Q""");
            
    $form->sendToPlayer($player);

            return 
    true;

        }

    }
    but if I do /contact and fill the form nothing inserts to database although I used the same code in another plugin and it works
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Call
    PHP:
    var_dump($this->CON()->error);
    after you query the database. What shows up in the console?
     
  3. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    string(0) ""
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    use 127.0.0.1 instead of localhost
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Uhm, I see what it is. You should probably read about callables. The problem is that you can't call `$this` inside a callable, very similar to this issue.

    Assuming you're new to PHP callables, I'd advise you create a separate class function for the form instead. Like so:
    PHP:
    public function onFormSubmit(Player $playerint $data null){
        
    $result $data;
        if (
    $result === null){return false;}

        switch (
    $result)
        {

            case 
    0:
                
    $this->report($player);
                break;

            case 
    1:
                
    $this->sugg($player);
                break;

            case 
    2:
                
    $this->inst($player);
                break;

            case 
    3:
                
    $this->yout($player);
                break;
        }
    }
    Now you will want that function to be called when the player submits the form. So you convert the class function into a callable by putting it in an array in such a way:
    PHP:
    $callable = [$yourClassInstance"yourClassFunction"];
    Like so:
    PHP:
    $form $api->createCustomForm([$this"onFormSubmit"]);
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.