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

Solved How to get text on database.

Discussion in 'Development' started by LewBr, Oct 20, 2017.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    :shoghi:
     
    Last edited: Oct 22, 2017
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    What SQL language are you using? The SQL shown here doesn't match SQLite3 or MySQL's format
     
  3. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    MySQL language database, can you give me an example how can i do that whit this language?
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    fair warning, it probably won't work, but try this:
    PHP:
    $escapedName mysqli::real_escape_string($sender->getName());
    /** @var mysqli_result|bool $result **/
    $result $this->plugin->provider->db->query("SELECT * FROM rede_geral WHERE lang = 'en' AND nome='{$escapedName}';");
    if(
    $result !== false) {
        
    $sender->sendMessage("Hello");
        return 
    true;
    }
    /** @var mysqli_result|bool $result **/
    $result $this->plugin->provider->db->query("SELECT * FROM rede_geral WHERE lang = 'pt' AND nome='{$escapedName}';");
    if(
    $result !== false) {
        
    $sender->sendMessage("Olá");
        return 
    true;
    }
    return 
    true;
     
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I don't know why but when i test the command it only say's "Hello", even being the database of the player in lang are 'pt'
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    A SELECT query without a syntax error always returns a mysqli_result. You have to fetch a row to find out the called, e.g.:
    PHP:
    $result $mysqli->query("SELECT COUNT(*) AS cnt FROM rede_geral WHERE lang = 'pt' AND name = '{$mysqli->escape_string($username)}'");
    $row $result->fetch_assoc();
    $result->close();
    $exists $row["cnt"] > 0;
    Nevertheless, you should always minimize the number of queries you send. So your whole code in the OP can be changed to a single SELECT statement:
    PHP:
    SELECT lang FROM rede_geral where nome 'escaped name here'
    Then check the result using PHP.
     
    jasonwynn10 and HimbeersaftLP like this.
  7. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I don't have understand so much this code, but when i test it says that 20.10 22:49:53 20.10 22:50:06 [Server] Server thread/CRITICAL Unhandled exception executing command 'lang' in lang: Undefined variable: lang
    PHP:
    20.10 22:49:53  20.10 22:50:06 [ServerServer thread/CRITICAL Unhandled exception executing command 'lang' in langUndefined variablelang
    $result 
    = $$this->plugin->provider->db->query("SELECT COUNT(*) AS cnt FROM rede_geral WHERE lang = 'en' AND nome = '{$this->plugin->provider->db->query->escape_string($sender)}'");
    Like, i really just want to have a function like "if player has lang = "en" then sendmessage("Hello!") but in database"
    Like this code in YML:
    PHP:
    $playerlang = new Config($this->getDataFolder() . "/languages.yml"Config::YAML);
                    
    $lang = new Config($this->getDataFolder() . "/lang.yml"Config::YAML); //Get database
                    
    $msg $lang->get($playerlang->get($pl->getName())); //Get player lang on database
                    
    $pl->sendMessage($msg["hi_message"]);
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    MySQLi is only used to execute queries and return values in rows.

    When you execute a SELECT query, it sends a request to the MySQL server, then waits for the response. The response from the MySQL server is called a "result set", which contains data in rows. For this query:
    Code:
    SELECT COUNT(*) AS cnt FROM rede_geral WHERE lang = 'en' AND nome = '$name'
    
    The MySQL server will return a result set with one row, with one column `cnt`. So the only column in the only row contains an integer, showing how many matches there are for "rede_geral WHERE lang='en' AND nome='$name'".

    However, you should optimize your queries. Here, you just want to know the lang of the player. You can download the player's lang into a variable, then compare the variable in PHP offline.

    In other words:
    PHP:
    $result $mysqli->query("SELECT lang FROM rede_geral WHERE nome = '{$mysqli->escape_string($player->getName())}'");
    $row $result->fetch_assoc();
    $result->close(); // close the mysqli_result if it is no longer used.

    if(is_array($row)){ // there is a row in rede_geral for this player
      
    $lang $row["lang"];
      if(
    $lang === "en"){
        
    // send message in English
      
    }elseif($lang === "pt"){
        
    // send message in Portuguese
      
    }
    }else{
      
    // rede_geral does not have any information for this player. What do you do now?
    }
     
    HimbeersaftLP and LewBr like this.
  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.