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

Tutorial MySQL in PocketMine plugins.

Comments in 'Resources' started by luca28pet, Mar 6, 2015.

  1. luca28pet
    Offline

    luca28pet Active Member

    Joined:
    Feb 20, 2014
    Posts:
    251
    Minecraft User:
    luca28pet
    Hey there 11!
    I decided to make this thread to show you how PocketMine-MP plugins can work with a MySQL database.

    PocketMine by default has MySQLi ("i" for "improved") extension installed, which allows you to work with databases like as usual.

    Let me show you an example:

    PHP:
    <?php

    namespace ImSQLPlugin;

    class 
    Main extends PluginBase
    {
       
        public function 
    onEnable()
        {
           
            
    # Credentials / Settings
           
            
    $host "127.0.0.1";
            
    $user "root";
            
    $password 123456;
            
    $databse "PMdata";
            
    $port 3306;
           
            
    # Connection using MySQLi
           
            
    $this->database = new \mysqli($host$user$password$database$port);
           
            
    # Check for connection error
           
            
    if($this->database->connect_error)
            {
                
    $this->getServer()->shutdown();
            }
           
            
    # Test query
           
            
    $this->databse->query("CREATE TABLE IF NOT EXISTS players (name VARCHAR(16) PRIMARY KEY, id INT)");
           
        }
       
        public function 
    onDisable()
        {
           
            
    # Remember to always close the datab
           
            
    $this->databse->close();
           
        }
       
    }
    That was the object oriented way.
    There is another way to connect and manage a MySQL database with procedural PHP:

    PHP:
    <?php

    namespace ImSQLPluginNotOOP;

    class 
    Main extends PluginBase
    {
       
        public function 
    onEnable()
        {
           
            
    # Credentials / Settings
           
            
    $host "127.0.0.1";
            
    $user "root";
            
    $password 123456;
            
    $databse "PMdata";
            
    $port 3306;
           
            
    # Connection using MySQLi
           
            
    $this->databse mysqli_connect($host$user$password);
           
            
    # Check for connection error
           
            
    if(!$this->database)
            {
                
    $this->getServer()->shutdown();
            }
           
            
    # Select the database
           
            
    mysqli_select_db($this->database$databse);
           
            
    # Test query
           
            
    $q "CREATE TABLE IF NOT EXISTS players (name VARCHAR(16) PRIMARY KEY, id INT)";
           
            
    mysqli_query($this->database$q);
           
        }
       
        public function 
    onDisable()
        {
           
            
    # Remember to always close the datab
           
            
    mysqli_close($this->databse);
           
        }
       
    }
    Well I hope I have explained all well.
    If you have suggestions/comments reply here.
    :)

Share This Page

Advertisement