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

Solved Trying to access array offset on value of type bool

Discussion in 'Development' started by minijaham, Jul 3, 2021.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Code:
    ErrorException: "Trying to access array offset on value of type bool" (EXCEPTION) in "plugins/Crates/src/minijaham/Crates/provider/SQLiteProvider" at line 46
    [17:51:45] [Server thread/CRITICAL]: #0 plugins/Crates/src/minijaham/Crates/provider/SQLiteProvider(46): pocketmine\utils\Utils::errorExceptionHandler(integer 8, string[51] Trying to access array offset on value of type bool, string[97] G:\Code Storage\PM3 Test Server 1\plugins\Crates\src\minijaham\Crates\provider\S, integer 46, array[4])
    
    Error code:
    PHP:
    /** @var Crates */
        
    private $plugin;

        
    /** @var SQLite3 */
        
    private $database;

        
    /**
         * SQLiteProvider constructor.
         *
         * @param Crates $plugin
         */
        
    public function __construct(Crates $plugin) {
            
    $this->plugin $plugin;
            
    $this->database = new SQLite3($plugin->getDataFolder() . "Players.db");
            
    $this->database->exec("CREATE TABLE IF NOT EXISTS players(uuid VARCHAR(36), username VARCHAR(16), crates TEXT DEFAULT '' NOT NULL);");
        }

        
    /**
         * @return SQLite3
         */
        
    public function getDatabase(): SQLite3 {
            return 
    $this->database;
        }

        
    /**
         * @param Player $player
         *
         * @return int[]
         */
        
    public function getCrateKeys(Player $player): array {
            
    $uuid $player->getRawUniqueId();
            
    $stmt $this->database->prepare("SELECT crates FROM players WHERE uuid = :uuid;");
            
    $stmt->bindValue(":uuid"$uuid);
            
    $result $stmt->execute();
            
    $crates $result->fetchArray(SQLITE3_ASSOC)["crates"];
            return 
    $this->plugin->decodeKeys($crates);
        }

        
    /**
         * @param Player $player
         *
         * @return bool
         */
        
    public function isRegistered(Player $player): bool {
            
    $uuid $player->getRawUniqueId();
            
    $stmt $this->database->prepare("SELECT username FROM players WHERE uuid = :uuid;");
            
    $stmt->bindValue(":uuid"$uuid);
            
    $result $stmt->execute();
            return 
    $result->fetchArray(SQLITE3_ASSOC)["username"] !== null true false// ERROR LINE

        
    }

        
    /**
         * @param Player $player
         */
        
    public function register(Player $player) {
            
    $uuid $player->getRawUniqueId();
            
    $username $player->getName();
            
    $stmt $this->database->prepare("INSERT INTO players(uuid, username) VALUES(:uuid, :username);");
            
    $stmt->bindValue(":uuid"$uuid);
            
    $stmt->bindValue(":username"$username);
            
    $stmt->execute();
            
    $this->plugin->getLogger()->notice("Registering {$player->getName()} into the quickCrates database!");
        }

        
    /**
         * @param Player $player
         */
        
    public function setCrateKeys(Player $player) {
            
    $uuid $player->getRawUniqueId();
            
    $session $this->plugin->getSessionManager()->getSession($player);
            
    $crates $this->plugin->encodeKeys($session->getKeys());
            
    $stmt $this->database->prepare("UPDATE players SET crates = :crates WHERE uuid = :uuid;");
            
    $stmt->bindValue(":crates"$crates);
            
    $stmt->bindValue(":uuid"$uuid);
            
    $stmt->execute();
            return;
        }
    This has been working in 1.16. What could be wrong? I'm using PHP7.4
     
  2. Axon

    Axon Zombie

    Messages:
    276

    $result->fetchArray(SQLITE3_ASSOC) Returns true/false
    Since I’m not an expert at pseudo, maybe add an if function to check if it returns a bool?

    Edit: I’ll get my pc to check :smile:
     
    Agent and minijaham like this.
  3. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Ah...thank you for the reply!

    I have no idea if this was the right fix for it but...
    PHP:
    /**
         * @param Player $player
         *
         * @return bool
         */
        
    public function isRegistered(Player $player) { // Removed : bool
            
    $uuid $player->getRawUniqueId();
            
    $stmt $this->database->prepare("SELECT username FROM players WHERE uuid = :uuid;");
            
    $stmt->bindValue(":uuid"$uuid);
            
    $result $stmt->execute();
            return 
    $result->fetchArray(SQLITE3_ASSOC)["username"] !== null true false;

        }
    Still works.
     
    Agent and Axon like this.
  4. Axon

    Axon Zombie

    Messages:
    276
    By still works, do you mean the error still exists, or it’s fixed :p
     
    Agent likes this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Fixed!
     
    Agent and Axon like this.
  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.