Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysqli_fetch_object creating objects with inaccessible properties #8068

Closed
ras52 opened this issue Feb 9, 2022 · 1 comment
Closed

mysqli_fetch_object creating objects with inaccessible properties #8068

ras52 opened this issue Feb 9, 2022 · 1 comment

Comments

@ras52
Copy link

ras52 commented Feb 9, 2022

Description

The following code fetches a object with a single property whose name and value is '42'.

<?php
$dbh = new mysqli('localhost', 'richard');
$res = $dbh->query('SELECT 42');
$obj = $res->fetch_object();

$prop = 42;
var_dump($obj);
echo ">{$obj->$prop}<\n";

$obj = json_decode(json_encode($obj));
var_dump($obj);
echo ">{$obj->$prop}<\n";

This results in this output:

object(stdClass)#3 (1) {
  [42]=>
  string(2) "42"
}
PHP Notice:  Undefined property: stdClass::$42 in /home/richard/test.php on line 8
><
object(stdClass)#4 (1) {
  ["42"]=>
  string(2) "42"
}
>42<

The first var_dump confirms that $obj does contain the expected property. However it is not possible to access it via the $obj->$prop syntax, nor with $obj->{42} or $obj->{'42'}. Transforming it to JSON and back turns it into a more regular object which can be accessed with either of these syntaxes.

I expected the >< in the output to read >42<. Changing $res->fetch_object() to (object)$res->fetch_assoc() has the expected effect, and provides an adequate work-around.

My guess is that this is another instance of this bug/oddity that was fixed in PHP 7.2 that escaped being fixed then.

Just in case its relevant, I'm testing with MariaDB 10.5.12.

PHP Version

PHP 8.1.2 (also 8.0.15 and 7.4.27)

Operating System

Debian 11

@ras52 ras52 changed the title mysqli_fetch_object creating objects with invalid properties mysqli_fetch_object creating objects with inaccessible properties Feb 9, 2022
@cmb69
Copy link
Member

cmb69 commented Feb 9, 2022

My guess is that this is another instance of this bug/oddity that was fixed in PHP 7.2 that escaped being fixed then.

Indeed.

cmb69 added a commit to cmb69/php-src that referenced this issue Mar 10, 2022
When fetching into objects, we need to create object style hash tables,
i.e. where numeric column names are stored as string keys instead of
integer keys.  Instead of the slightly more efficient alternative to
create the desired hash table in the first place, we go for the more
readable implementation and convert the array style hash table using
`zend_symtable_to_proptable()`.
@cmb69 cmb69 self-assigned this Mar 10, 2022
@cmb69 cmb69 closed this as completed in ef29ddc Mar 14, 2022
cmb69 added a commit that referenced this issue Mar 14, 2022
* PHP-8.0:
  Fix GH-8068: mysqli_fetch_object creates inaccessible properties
cmb69 added a commit that referenced this issue Mar 14, 2022
* PHP-8.1:
  Fix GH-8068: mysqli_fetch_object creates inaccessible properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@ras52 @cmb69 and others