|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-03-04 17:21 UTC] uwendel at mysql dot com
Description:
------------
What kind of thing is the PDORow::queryString property?
PDORow objects are generated and returned by PDOStatement::fetch(PDO::FETCH_LAZY). PDORow objects seem a bit special in some ways.
1) "PDO::FETCH_LAZY = PDO::FETCH_OBJ + PDO::FETCH_BOTH"
PDO::FETCH_BOTH means that the returned data is both indexed by the column name and a column offset number. For example, a query like SELECT id FROM test should return an object (resp. array) with the properties {0} and id. You can access both properties and you get what you want. But var_dump() will report only the column name based property and not the property based on the numeric column offset.
I have no idea if this is a var_dump() or a PDO flaw.
2) The magic queryString property
var_dump() reports a queryString property for PDORow objects returned by PDOStatement::fetch(). In all my testing I found the queryString propery value to be the query string which has constructed the corresponding PDOStatement object. However, I cannot access this property. I see it, but I have no access. That makes it a bit "magic".
Also, note how var_dump(), var_export() and debug_zval_dump report different things. Johannes already explained to me that the functions might do different things, but anyway it looks confusing to me.
Reproduce code:
---------------
------------------------ 1, FETCH_LAZY and numeric offset ----------
sapi/cli/php -r '$db = new PDO("mysql:dbname=phptest;unix_socket=/tmp/mysql.sock", "root", "root"); $stmt = $db->prepare("SELECT 1 AS \"one\""); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_LAZY); var_dump($row); var_dump($row->{0}); var_dump($row->one); '
object(PDORow)#3 (2) {
["queryString"]=>
string(17) "SELECT 1 AS "one""
["one"]=>
string(1) "1"
}
-->
string(1) "1"
string(1) "1"
-------------------- 2, magic PDORow::queryString() ---------
sapi/cli/php -r '$db = new PDO("mysql:dbname=phptest;unix_socket=/tmp/mysql.sock", "root", "root"); $stmt = $db->prepare("SELECT 1 AS \"one\""); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_LAZY); var_dump($row); var_dump($row->queryString); '
object(PDORow)#3 (2) {
["queryString"]=>
string(17) "SELECT 1 AS "one""
["one"]=>
string(1) "1"
}
-->
UNKNOWN:0
sapi/cli/php -r '$db = new PDO("mysql:dbname=phptest;unix_socket=/tmp/mysql.sock", "root", "root"); $db->exec("DROP TABLE test"); $db->exec("CREATE TABLE test (id INT)"); $db->exec("INSERT INTO test(id) VALUES (1)"); $stmt = $db->prepare("SELECT id FROM test"); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_LAZY); var_dump($row); var_dump($row->queryString); var_export($row->queryString); print "\n"; debug_zval_dump($row->queryString);'
-->
UNKNOWN:0
NULL
UNKNOWN:0
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 08:00:02 2025 UTC |
Crash with CVS snapshot nixnutz@ulflinux:~/php53_libmysql> sapi/cli/php -v PHP 5.3.0-dev (cli) (built: Apr 11 2008 12:02:49) Copyright (c) 1997-2008 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies nixnutz@ulflinux:~/php53_libmysql> sapi/cli/php -r '$db = new PDO("sqlite:/tmp/foo"); $stmt = $db->query("SELECT 1"); $row = $stmt->fetch(PDO::FETCH_LAZY); var_dump($row->{0}); var_dump($row->queryString); get_class($row);' string(1) "1" string(1) "1" Speicherzugriffsfehler