Replit DB in PHP is not working

Question:
I am using @MattDESTROYER’s ReplDB for PHP and I have assigned a key in the db to an array. When trying to access this key, it throws the following error:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /home/runner/Elemental-Conquest/play/map/plains/index.php:19 Stack trace: #0 {main} thrown in /home/runner/Elemental-Conquest/play/map/plains/index.php on line 19

Repl link: https://replit.com/@element1010/Elemental-Conquest

$db = new ReplDB(getenv("REPLIT_DB_URL"));

  if($db->get("plains") == "key fetching failed") {
    $db->set("plains", array( // Sectors
      array(
        "owner" => "__none",
        "buildings" => array(
          "none",
          "none",
          "none"
        ),
        "buildingSlots" => 3
      )
    ));
  }
1 Like

I was completely wrong, but I figured it out. You’re trying to use an object as an associative array, instead of echo $db->get("plains")[0]["owner"]; you need to do echo $db->get("plains")[0]->owner.

3 Likes

But I created it as an associative array, so how did it become an object?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.