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

MySQL Problem

Comments in 'Plugin Development' started by Flabe, Mar 19, 2016.

  1. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    Hello guys, i am learning on how to use database in my pocket mine plugins, but i have a problem, maybe some problem in my SQL (because there is no errors in console), so i need your help to correct me, thanks)) The problem is: When player comes on server for the first time it don't add( register) player to database(((
    PHP:
    public function isRegistered($player){
        
    $name trim(strtolower($player->getName()));

            
    $result $this->database->query("SELECT * FROM 'players' WHERE name = '" $this->database->escape_string($name)."'");

    if(
    $result instanceof \mysqli_result){
    if(
    count($result) > 0){
    return 
    true;



    }else{
    return 
    false;
    }
    }
    }
    public function 
    registerPlayer($player){    
    $name trim(strtolower($player->getName()));    
    $this->database->query("INSERT INTO 'players' (name, coins) VALUES ('".$this->database->escape_string($name)."', 0);");
        return 
    true;

    }
    public function 
    PlayerJoin(PlayerJoinEvent $event){
            
    $tpg $event->getPlayer();
            if (
    $this->isRegistered($tpg) == false){
    $this->registerPlayer($tpg);

    }

             
        }

    ------------------------------------------------------------------------
    Thank you for all your answers, you are really helping me, don't type something like: 'LEARN PHP' because i am still learning now)
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Connection to MySQL is established?
    PHP:
    mysql_connect("$host","$username","$password") or die("cannot connect");
    mysql_select_db("$database") or die("cannot select DB");
  3. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    here you are my code, connection works
    PHP:
    $this->database = new \mysqli($host$user$password$database$port);
                if(
    $this->database->connect_error)
            {
                
    $this->getServer()->getLogger()->info(TextFormat::GREEN."[SSM] Ошибка подключения!");
            }else{
                    
    $this->getServer()->getLogger()->info(TextFormat::GREEN."[SSM] Подключён к базе данных!");
                }
                
    $this->database->query("create table IF NOT EXISTS players (
      id integer auto_increment primary key,
      name varchar(200) not null,
      coins integer)"
    );
        }
  4. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    any other suggestions?
  5. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    Please guys, answer me((
  6. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    nobody?(((
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Not pro in SQL Syntax
    PHP:
    ("INSERT INTO 'players' (name, coins) VALUES ('".$this->database->escape_string($name)."', 0);");
    To
    PHP:
    ("INSERT INTO `players` (`name`, `coins`) VALUES ('{$this->database->escape_string($name)}', '0');")
    PEMapModder likes this.
  8. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    thanks ill try it)
  9. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    PHP:
    it not works((((
  10. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    i tried all , nothing works(((
  11. Flabe
    Offline

    Flabe Active Member

    Joined:
    Nov 17, 2015
    Posts:
    147
    Minecraft User:
    Flabe231
    OMG I SOLVED IT I SPENT SO MANY TIME!!!! THANK YOU PRIMUS FOR HELPING ME I LOVE YOU GUYS YOOOOOHOHOHOHOHOHOO!!!
  12. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    No problem. Next time handle errors
    PHP:
        if($e $this->database->error){
           
    $plugin->getLogger()->warning("MySQL Error: ".$e);
         }
    PEMapModder and Legoboy0215 like this.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Use isset.
  14. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    isset is faster?
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Nope. But sometimes $mysqli->error is undefined rather than null. So I think that using isset is better (safer), since it returns true if error is null anyway.
    Primus likes this.
  16. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    This looks better.
    PHP:
    if (!$mysqli->query("SET a=1")) {
       
    printf("Errormessage: %s\n"$mysqli->error);
    }
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Indeed, but better store the result and use `=== false` as good practice.

Share This Page

Advertisement