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

How works variables and databases query?

Discussion in 'Off-Topic' started by LewBr, May 3, 2018.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    It will be more faster if i register a variable als insert what is in more times with database query and another searching tools?
    I know that is a robot and not an human but when i register a query on variable they get only one time or everytime when i write the variable? e.g:
    PHP:
    $var $this->plugin->getDatabaseOverall($player->getName())["something"]; //run the query database
    if($var == 1//run the query again? while using $var to get
    {
    //blabla
    }elseif($var == 2//run the query again 2 times? while using $var for result

    //blabla
    }
    and i have another question for the mysql masters like SOFe.. what would be the best way to use databases on pmmp? I use something like this, but when i run the function it will accumulate something more on my server? What would be the best way to run that and then close for no accumulations on my server?
    the function:
    PHP:
    public function getDatabaseOverall($player){
        
    $stmt $this->db->prepare("SELECT `something`, `another_something` FROM `database` WHERE `nickname` = ?");
        
    $stmt_player strtolower($player);
        
    $stmt->bind_param("s"$stmt_player);
        if(
    $stmt->execute()){
                            
    $stmt->bind_result($something);
                            
    $stmt->fetch();
                            
    $data = array(
                                
    "something" => $something,
                                
    "another_something" => $another_something
                               
                            
    );
                            
    $stmt->close();
                            return 
    $data;
                        }else{
                            
    $stmt->close();
                        }
        }
    I am just learning how PHP and another functions work on machine so please don't kill me on comments, I appreciate all helpful comments. :)
    (medium/bad english srry)
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    There is one thing to be said about MySQL. It IS definitely fast, but it's a bad idea to use it on the main thread for production where the database gets queried frequently and the querying isn't spam-proof.

    I'm assuming you're running the code without parallelization from the main thread as per your first code snippet.

    MySQL is slow, yet fast for large-scale data fetching.

    Let's say it takes 0.01 seconds to fetch 2 rows from a table containing 100 elements.
    It would take nearly as much (or just as much) if the table were to contain 100,000 elements.

    0.01 seconds is pretty slow (actually, your queries would definitely be faster than 0.01 if you've optimized your tables) but it seems like you will need to pay an initial cost before running a query.

    Oh, and that strtolower check isn't needed, or at least if your `player` column is a VARCHAR.
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    SOFe's libasynql seems to handle MySQL queries better than the generic AsyncTask way most of the plugins do. Maybe that virion could ease it for you.
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Learn PHP. $var is a variable, and when you compare a value to a variable, it only retrieved the stored value of the variable. It doesn't recalculate the variable.
     
    Primus likes this.
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Yeah, that's why i said i'm learning that language.
    Thank you, good informations, so and how do i optimize my database better? Longer text's like SkinData player, it should be used a Big Data for that, because with mysqli is not a good way, that's what i heard and it's a bit obviously, do you have some another recommendations?
    Well, i don't want to use libraries it's such big for me and i just want to get something more basic, we don't use another plugins like that, it's just a Core like LifeBoat does and another servers, but it's good to see how this librarie does and works, thanks.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Depends if there's a need to save skin data twice :oops:
    PocketMine saves skin data in players/playername.dat file, you can read it using BigEndianNBTStream.

    Anyway, this should help you: https://dev.mysql.com/doc/refman/8.0/en/optimizing-database-structure.html
     
    LewBr likes this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Building a core doesn't mean libraries aren't suitable for you ;) You could simply copy the library code into your src folder and edit libasynql.php (change isPackaged() to return true so that debug mode isn't enabled) if you don't want to go through the phar shading process :)
    Do you need to access it from other clients through the MySQL server? What about storing the skin in another file somewhere on your machine, then just store the absolute path of the file in the database?
     
    LewBr and Primus like this.
  8. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Yeah i need to get on my website that SkinData, but actually i don't have any idea how to do that because everytime when i get a data skin of a player and convert to base64_encode i don't know how to get that data to be like an png image for my website..
    and that librarie i don't know if should i use that, but i will see if it's a good idea with the others, thanks SOFe.
     
  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.