Skip to content

VenityNetwork/MysqlLib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MysqlLib

A Mysql library for PocketMine-MP.

  • Asynchronous
  • Custom Query
  • Multiple Threads/Connections

Usage

  1. Initialization
$threads = 2; // threads count, increase this if the query is slow and prevent blocking query
$db = MysqlLib::init($plugin, new MysqlCredentials(
    host: "127.0.0.1",
    user: "root",
    password: "",
    db: "database",
    port: 3306
), $threads);
  1. Create a first query
class GetEmailQuery extends MysqlQuery{

    public function execute(MysqlConnection $conn, array $params): mixed{
        $result = $conn->select('SELECT email FROM users WHERE name=? LIMIT 1', $params["name"]);
        $row = $result->getOneRow();
        if($row !== null) {
            return $row["email"];
        }
        return null;
    }
}
  1. Call the query
$db->query(GetEmailQuery::class, ["name" => "John_Doe"], function($email) {
    var_dump($email);
},
function() {
    var_dump("ERROR");
}
);

Simple Usage

  • Select (Return: rows)
/** @var \VenityNetwork\MysqlLib\MysqlLib $db */
$db->rawSelect("SELECT * FROM players WHERE xuid=?", [$xuid], function(?array $rows) {
    var_dump($rows);
}, function(string $errorMessage) {
    var_dump("Error: {$errorMessage}");
});
  • Insert (Returns: affected rows, last insert id)
/** @var \VenityNetwork\MysqlLib\MysqlLib $db */
$db->rawInsert("INSERT INTO players (xuid) VALUES (?)", [$xuid], function(int $affected_rows, int $insert_id) {
    var_dump("Affected Rows: {$affected_rows}, Insert ID: {$insert_id}");
}, function(string $errorMessage) {
    // todo
});
  • Update / Delete (Return: affected rows)
/** @var \VenityNetwork\MysqlLib\MysqlLib $db */
$db->rawChange("UPDATE players SET money=? WHERE xuid=?", [$money, $xuid], function(int $affected_rows) {
    var_dump("Affected Rows: {$affected_rows}");
}, function(string $errorMessage) {
    // todo
});
  • Generic (No result returned)
/** @var \VenityNetwork\MysqlLib\MysqlLib $db */
$db->rawChange("CREATE TABLE `players` (`xuid` VARCHAR(16), `money` BIGINT) PRIMARY KEY (`xuid`)", function(bool $success) {
    // todo
}, function(string $errorMessage) {
    // todo
});

About

Mysql Library for PocketMine-MP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages