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

libasynql get the number of rows resulted

Discussion in 'Development' started by Levi, Apr 11, 2019.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how do i get the number of rows resulted in libasynql?

    Trying to check if player exists in mysql database through select * from db where uuid=:uuid

    But how do I get how many numbers of rows resulted from the query

    PHP:
    public function accountExists Player $player, callable $callable )
            {
             
                if ( !
    $player instanceof Player ) {
                    return;
                }
                
    $uuid $player->getUniqueId();
                
    $this->connector->executeSelectQueries::GET_ALL, [
                    
    "uuid" => (string)$uuid,
                ], 
    $callable );
             
            }
    PHP:
    public function join(PlayerJoinEvent $event){
    $this->provider()->accountExists$player,
                 
                    function ( array 
    $result ) use ( $player$uuid) {
                     
                        if ( 
    $result number of rows 0  ) {

                        
    // create player
                         
                        
    } else {
                         
                           
    // store player to array
                         
                        
    }
                    } );
    }
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Select COUNT(*) instead of selecting the rows. Otherwise you are downloading the whole table.
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    How do i check the num rows with that
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    All I am doing this for is to check if the player is in the database. Should I just use
    if(!$player->hasPlayedBefore()){ // and then create account
    instead of querying mysql?
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    COUNT(*) is an aggregate function that returns the number of rows matching the query. It always returns exactly one row, containing the count.

    Never use num_rows if you are just interested in the number of rows available; use COUNT(*) for that case. num_rows is used when you have not fetched all the rows, but libasynql fetches everything asynchronously first
     
  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.