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

Solved how can I connect MySQL with plugin

Discussion in 'Development' started by abdullah, May 23, 2018.

  1. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    I tried to connect to MySQL database using PDO and mysqli

    using msqli :
    PHP:
    <?php

    namespace ImSQLPlugin;

    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase
    {

        public function 
    onEnable()
        {

            
    $host "localhost";
            
    $user "root";
            
    $password "";
            
    $database "PMdata";
            
    $port 3306;

            
    $this->database = new \mysqli($host$user$password$database$port);

            if(
    $this->database->connect_error)
            {
                
    $this->getServer()->shutdown();
            }

            
    $this->databse->query("CREATE TABLE IF NOT EXISTS players (name VARCHAR(16) PRIMARY KEY, id INT)");

        }

        public function 
    onDisable()
        {

            
    $this->databse->close();

        }

    }
    if I use mysqli I get that error in console :
    [Server thread/CRITICAL]: Error: "Call to undefined function ImSQLPlugin\mysqli_connect()" (EXCEPTION) in "ImSQLPlugin/src/ImSQLPlugin/Main"

    using PDO :
    PHP:
    <?php

    namespace ImSQLPlugin;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as Color;

    class 
    Main extends PluginBase
    {

        public function 
    onEnable()
        {

            
    $this->getLogger()->info(Color::GREEN "ImSQLPlugin has Enabled");

            
    /* start sql connection */

            
    $dsn 'mysql:host=localhost;dbname=PMdata';
            
    $user 'root';
            
    $pass '';

            try
            {

                
    $db = new \PDO($dsn$user$pass);
                
    $this->getLogger()->info(Color::GREEN "you are connected");

            }catch (\
    PDOException $e)
            {

                
    $this->getLogger()->info(Color::RED "error :" $e->getMessage());

            }

            
    /* endc sql connection */

        
    }

        public function 
    onDisable()
        {

            
    $this->getLogger()->info(Color::RED "ImSQLPlugin has Disabled");

        }

    }
    if I use PDO I get that error in console :
    [Server thread/CRITICAL]: Error: "Class 'PDO' not found" (EXCEPTION) in "ImSQLPlugin/src/ImSQLPlugin/Main"

    how can I fix that please ?
     
  2. PocketKiller

    PocketKiller Slime

    Messages:
    83
    GitHub:
    iPocket
    Clearly, the code doesn't match the error. I cannot see any mysqli_connect().
    Install the PDO extension depending on your host specifications.
     
  3. This is because he only included the snippet that he/she felt was needed to solve the issue.
     
  4. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    how can I do that ?? can you give me site or video tech me that
     
  5. Try Google?
     
  6. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    I did not find anything
     
  7. Try harder, it is there.
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What OS are you in?

    Also check the php.ini of your PHP binaries. There may be an ;extension=mysqli line. Try removing the semicolon.

    [​IMG]
     
    RyanShaw and HimbeersaftLP like this.
  9. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Use 127.0.0.1 instead of localhost
     
  10. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    Windows 10
     
  11. abdullah

    abdullah Spider

    Messages:
    14
    GitHub:
    x3boodx
    thank you it was solved i just do that
     
  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.