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

How to use MySQL

Comments in 'Plugin Development' started by MyNameIsTriXz, Jun 3, 2016.

Thread Status:
Not open for further replies.
  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Hey, I am planning to add MySQL to my server, to save player information like kills, wins and kits. I already have experience with file saving, but saving more than 10.000 player files and open them often is not that good for your server's performance. So I'd like to ask which kind of MySQL is the best one, I heard that MySQLi is good, but why? I don't really know how to setup a MySQL database, can I create one by code or do I need to create it on phpmyadmin.com? Can you give me some code to experiment with, which connects to localhost and which gets and sets a value "kill" on the database. And please tell me how you have learnt MySQL / MySQLi, like which webpage.


    Thanks.
  2. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I suggest you check google, this is not a SQL forum.

    Anyway, you can get a database connection with this.

    PHP:
    public function getDatabase() {
             new \
    mysqli("ip""user""password""database");
    }
    If you're plugin has also Yaml/Json/... support, you should use a structure controller (as if) to check that it's enabled, or your plugin will return an error.
    Also, is highly recommendable do a AsyncTask checking if the MySQL database is online.

    And, also, to get "kills" and "coins" values from the table luna_de_pluton (as example) and the player Pedro, you can use mysqli_fetch_array function.
    Example:

    PHP:
    $array $this->getDatabase()->query("SELECT kills, coins FROM luna_de_pluton WHERE user='Pedro'");
    $array mysqli_fetch_array($array);
    echo 
    $array[0]; // return Pedro's kills
    echo $array[1]; // return Pedro's coins
    And, to initialize the database without use "phpmyadmin"... Let's try searching in php.net! :p
Thread Status:
Not open for further replies.

Share This Page

Advertisement