|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-01-10 10:38 UTC] r dot schneider at artworx dot at
Description: ------------ --- From manual page: http://www.php.net/pdostatement.fetch --- With MySQL all those fetch methods provide only strings. All integers in the table have to be converted manually. This should be noted somewhere. I didn't know this for long time and I didn't have any problem with this because of the implicit conversioning in php. I detected it just recently. I don't understand why PDO behaves like this. This makes the proper usage of fetchObject('myClass') not possible. Maybe I have a wrong understanding of how to use php with its implicit conversions. Should I avoid distinguishing between data types, should all primitive data type operations work also with strings? At least I would expect that the documentation clearly states this fact. But better would be if the correct data types gets used. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 14:00:01 2025 UTC |
How does this make "the proper usage of fetchObject('myClass') not possible"? The reason is that the MySQL Server sends strings to PHP. PHP doesn't touch it further. If you use native prepared statments (PDO uses an emulation by default) MySQL switches to a different protocol and you get more native types (while even that doesn't cover all types properly - thing MySQL DECIMAL, there is no precise numeric type in PHP for non-integers)What are native prepared statements? Do you mean stored procedures? Let me ask in another way. How could an object easily gets populated with the correct data types? /** This class has the same/equivalent data types as in an corresponding table */ class A { /** @var int $id */ private $id; /** @var string $name */ private $name; /** @var float $size */ private $size; /** @var DateTime $lastLogin */ private $lastLogin; } DateTime might be a bit more sophisticated. But the primitive data types should get the right types. But this is not possible with fetchObject('A'). All member variables will be strings. Should fetchObject not be used that way? Should it be used only for fool data classes (classes without methods)? Or is this not the php style? Should I 'ignore' primitive data types? What would mean fetchObject('A') is okay. Or is it intended that fetchObject is only for 'string classes', or that the call usually needs some rework? Wouldn't it be good if we could get the correct data types? Maybe it is my lack of knowledge. If so then sorry for the ticket. But I never came across anywhere that told me that MySQL/PDO returns strings only and, because of that, to use it in a certain way. That's why I suggested to put this into the documentation. Thank you!