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

[Question] Using usual MySQL/MySQLi commands

Comments in 'Plugin Development' started by mamayadesu, Feb 22, 2015.

  1. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    I can use usual MySQL or MySQLi commands in plugins? Example:
    PHP:
    public function onEnable(){
    $link mysqli_connect("localhost""root""********""database");
    $var mysqli_query($link"SELECT * FROM `".$this->getConfig()->get("mysqltable")."` WHERE `name`='".$event->getPlayer()->getName()."'");
    $var mysqli_fetch_array($var);
    $this->getLogger()->info($var['name']);
    }
  2. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That's correct. You need to close/free $var though.
  4. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    PHP:
    public function onEnable(){
    $link mysqli_connect("localhost""root""********""database");
    $var mysqli_query($link"SELECT * FROM `".$this->getConfig()->get("mysqltable")."` WHERE `name`='".$event->getPlayer()->getName()."'");
    $var mysqli_fetch_array($var);
    $this->getLogger()->info($var['name']);
    mysqli_close($link);
    }
    ?
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually, to boost the server's performance, I recommend you:
    - open connection in onEnable()
    - every time you fetch data, remember to close the result
    - close connection in onDisable()
  6. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    How to close result?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not sure, but if you use the object-oriented style, it is $mysqli_result->close()
  8. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    This var from pocketmine?
  9. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Obviously not. Ive been using the non OOP method of mysqli before. I find it messy if you're working on a bigger scale. learn OOP instead. Like this:
    $db = new \mysqli(/*mysql_data*/);
    $db->query(); //if you want to query something.
    $db->query()->fetch_assoc(); //if u want to fetch query results

    Btw, heres a little hint(works on few):
    mysqli_query() => $db->query()
    if you notice, the "_" in the mysqli_query() function, it acts like "->". its not pretty accurate but helps me with this pattern. The best way is to google for mysqli php OOP examples.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP.net/mysqli

Share This Page

Advertisement