"Undefined offset: 0

Discussion in 'Development' started by Levi, Mar 7, 2019.

  1. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    Using libasynql
    Code
    PHP:
    /**
         * @param callable $callable
         * @param Player $player
         * @return float
         */
        
    public function getMoney(Player $player, callable $callable)
        {
            if (!
    $player instanceof Player) {
               return 
    true;
            }
            
    $uuid $player->getUniqueId();
            
    $result $this->connector->executeSelect(Queries::GET_BALANCE, [
                
    "uuid" => (string) $uuid
            
    ], $callable);
            return 
    $result;
        }
    mysq.sql
    PHP:
    -- #    {get
    -- #      {balance
    -- #      :uuid string
    SELECT BALANCE FROM players WHERE UUID=:uuid;
    -- 
    #      }
    -- #   }
    and when i call it
    PHP:
    $this->getPlugin()->getProvider()->getMoney(
                
    $sender,
                function (array 
    $rows) use ($sender$uuid) {
                        
    $sender->sendMessage("balance - ".$rows[0]["BALANCE"]);
            });
    gives [19:15:57] [Server thread/CRITICAL]: ErrorException: "Undefined offset: 0" by the (EXCEPTION)
     
  2. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    Seems like normal behaviour. If $rows is empty then the player's record doesn't exist in the "players" table.
     
  3. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    It does exist.
    I created the uuid column as binary(16) though. Is that why
     
  4. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    I store uuids in BINARY(16) too but haven't had any issues.
     
  5. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    Ah, I see. So this is where the issue is:
    (string) $uuid converts the UUID to those readable text uuids that contain hyphens. Since you store them in binary, you need to
    PHP:
    "uuid" => $uuid->toBinary()
    P.S. The reverse of UUID->toBinary() is UUID::fromBinary(). Also, $player->getRawUniqueId() is a shorthand for $player->getUniqueId()->toBinary().
     
    wolfdale likes this.
  6. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    Will it make any difference if i make the uuid column varchar instead of binary and save uuid as string ?
     
  7. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    You could store UUIDS as CHAR(36) yeah. You'll need to change your UUID implementation back to (string) $uuid for this. Binary strings I could say have at least two benefits:
    • They take lesser storage space and are definitely faster to look up. Though I'm not sure if the performance impact by storing UUIDs as CHAR is that significant.
    • PMMP has a method to fetch a player directly (O(1)) by their binary UUID - Server::getPlayerByRawUUID($uuid). There is a method to fetch player by a UUID object as well but thats basically converting UUID object to binary and calling getPlayerByRawUUID. Yes, there's Server::geyPlayerExact($nams) as well but that in it's worst case is O(n) as it iterates over every online player (or at least attempts to).
    P.S. MySQL 8 has UUID_TO_BIN(binary UUID) and BIN_TO_UUID(string UUID) to convert between the two forms of UUID.
     
  8. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    This is a bit different, but if i want to print out the player's skin on my website and on my pm server, what should be my skin data column be? Mediumblob(16)?
     
  9. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    Yeah, that will work but you can't specify a size for BLOBs. So it's just MEDIUMBLOB.
     

Share This Page

  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.