1

When using mysqli_fetch_object(), you can pass the name of an object as the second parameter. By doing so, mysqli creates the corresponding object and automatically sets the variables.

objectname {

// No defined properties

}

(...)

$object = mysqli_fetch_object($result, "objectname");

Under the assumption that $result contains data for "forname" and "surname", we would now have access to:

$object->forename;
$object->surname;

Is it possible to fetch the data into an associative array instead? Like shown here:

objectname {
    public $data = array();
}

(...)

$object = mysqli_fetch_object($result, "objectname");

And then having:

$object->data["forename"];
$object->data["surname"];

If not: What is the code, MySQLi uses to populate the object with mysqli_fetch_object()? Considering that MySQLi is even able to change predefined private variables, it is a total mystery for me.

10
  • Can you clarify what you are after as there have been a few answers which have been removed due to what is a perceived misunderstanding. Do you want to know how the code has access to private variables, how to return the result set as an array instead? Commented Jan 30, 2021 at 12:50
  • @NigelRen The goal is to fetch an object with the data as an array that is a property of the object. Commented Jan 30, 2021 at 12:52
  • So isn't that something like mysqli_fetch_array() - followed by assign to object(constructor or otherwise)? Commented Jan 30, 2021 at 12:55
  • Correct me if i'm wrong, but I think the only way to do this would be to create an object, then do:-----------"$object->data = mysqli_fetch_assoc($result)" basically as @NigelRen has just said, creating the object and assigning the assoc array to it Commented Jan 30, 2021 at 12:55
  • 2
    @Dharman, being passed to a constructor or set method would allow control over the scope of the data. My point has been that if current answers have misunderstood the issue, I wanted to try and clarify it to help in useful answers being added (hopefully). Commented Jan 30, 2021 at 12:58

1 Answer 1

1

Unfortunately, you can't do it with mysqli. Usually, I would recommend PDO in such situations but even PDO doesn't have such capability.

There is a workaround though. You can create your own class with a constructor and a private property.

class MyClass
{
    public function __construct(private array $data) {}
}

$stmt = $mysqli->prepare('SELECT id, name FROM users');
$stmt->execute();
$result = $stmt->get_result();

$data = [];
foreach ($result as $row) {
    $data[] = new MyClass($row);
}
var_dump($data);

In the code above I am iterating over the result set and creating a new object passing the row each time as a constructor argument. You can control the visibility of the property using this approach.

P.S. If you are using PHP 7 then you can define the class this way:

class MyClass
{
    private array $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.