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

Solved SQL error

Comments in 'Plugin Development' started by Asparanc, Sep 5, 2015.

  1. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    PHP:
    $this->database = new \mysqli($this->config["host"],$this->config["user"],$this->config["pass"],    $this->config["bdd"]);   
    if (
    $this->database->connect_error) {           
    throw new \
    RuntimeException("Invalid MySql settings");           
    return;   
        }   
        
    $sql "CREATE TABLE IF NOT EXISTS Users (            player VARCHAR(16) NOT NULL, password    TEXT NOT NULL,            argent INT NOT NULL,            PRIMARY KEY (player)        )";   
        
    $this->database->query($sql);   
        
    Server::getInstance()->getLogger()->info("Connected to MySQL server");
    The plugin connect to the database but the table is not created, why ?
  2. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    It work thank you ! :)
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Check if the result of query() is identical to false. If it is, throw an exception with $database->error.
    Asparanc likes this.
  4. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    That's a solution ^^ it's my first time for Mysqli, i always work with PDO.
  5. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    I'm tired :/

    PHP:
    public function onJoin(PlayerJoinEvent $event){
     
    $level $event->getPlayer()->getLevel()->getName();
    $player $event->getPlayer();
    if (
    $this->isRegistered($player) == false){
    $this->registerPlayer($player);

    }

    }


    PHP:
    public function isRegistered($player){
        
    $result $this->database->query("SELECT * FROM Users WHERE player = '" $this->database->escape_string($player->getName())."'");

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



    }else{
    return 
    false;
    }
    }
    }
    public function 
    registerPlayer($player){       
    $name trim(strtolower($player->getName()));   
    $data = [           
    "password" => "",       
        
    "argent" => 0,       
        
    "classe" => "undefined",   
            
    "grade" =>     "Joueur"
        
    ];       
    $this->database->query("INSERT INTO Users            (player, password, argent, classe, grade)            VALUES            ('".$this->database->escape_string($name)."', ".$data["password"].", ".$data["argent"].", ".$data["classe"].", '".$data["grade"]."')        ");   
        return 
    true;   

    }



    Why player isn't register after joining ?
  6. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    1. You need to put quotes around the strings in your query
    2. [​IMG]
    Asparanc likes this.
  8. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    1. I've resolved the thread i've forgot to add the prefix Solved ^^

Share This Page

Advertisement