NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 guru: help fix simple PEAR DB problem

Daniel Convissor danielc at analysisandsolutions.com
Sun Feb 1 19:07:19 EST 2004


Hi Folks:

I just uploaded the latest release of PEAR DB, 1.6.0RC3.

Can someone who's more familliar with PHP 5 than I please help fix the 
following problem?

The DB_common::getAssoc() method works fine in PHP 4 but is producing
really weird results under PHP 5.  If you're wondering, PHP 5 is a windows
snapshot from today.

Attached are three files:

1)  getassoc.out.diff is a side-by-side diff of the tests run under PHP 4
and PHP 5. See the "ABOUT TO BE RETURNED" section at the bottom for the 
diff.

2)  common.php.getassoc.diff is a unified diff for patching the current 
DB/common.php file to provide the debugging info.

3)  getassoc.phpt is a test file.

Thanks so much,

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
-------------- next part --------------
Index: common.php
===================================================================
RCS file: /repository/pear/DB/DB/common.php,v
retrieving revision 1.75
diff -u -r1.75 common.php
--- common.php	31 Jan 2004 01:09:35 -0000	1.75
+++ common.php	1 Feb 2004 23:51:47 -0000
@@ -1361,11 +1361,19 @@
                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
                     $arr = get_object_vars($row);
                     $key = current($arr);
+print "\n--------------\narr...\n";
+print_r($arr);
+print "\nkey = $key\n";
+print "\nrow...\n";
+print_r($row);
                     if ($group) {
                         $results[$key][] = $row;
                     } else {
                         $results[$key] = $row;
+print "\nresults[$key]...\n";
+print_r($results[$key]);
                     }
+print "--------------\n";
                 }
             } else {
                 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
@@ -1398,6 +1406,9 @@
 
         $res->free();
 
+echo "\n\$result ABOUT TO BE RETURNED...\n";
+print_r($results);
+
         return $results;
     }
 
-------------- next part --------------
4.3.5RC2-dev						      |	5.0.0RC1-dev
testing getAssoc with false force, array params, DB_FETCHMODE	testing getAssoc with false force, array params, DB_FETCHMODE

--------------							--------------
arr...								arr...
Array								Array
(								(
    [a] => 42							    [a] => 42
    [b] => bing							    [b] => bing
    [c] => This is a test					    [c] => This is a test
)								)

key = 42							key = 42

row...								row...
stdClass Object							stdClass Object
(								(
    [a] => 42							    [a] => 42
    [b] => bing							    [b] => bing
    [c] => This is a test					    [c] => This is a test
)								)

results[42]...							results[42]...
stdClass Object							stdClass Object
(								(
    [a] => 42							    [a] => 42
    [b] => bing							    [b] => bing
    [c] => This is a test					    [c] => This is a test
)								)
--------------							--------------

--------------							--------------
arr...								arr...
Array								Array
(								(
    [a] => 42							    [a] => 42
    [b] => three						    [b] => three
    [c] => Three						    [c] => Three
)								)

key = 42							key = 42

row...								row...
stdClass Object							stdClass Object
(								(
    [a] => 42							    [a] => 42
    [b] => three						    [b] => three
    [c] => Three						    [c] => Three
)								)

results[42]...							results[42]...
stdClass Object							stdClass Object
(								(
    [a] => 42							    [a] => 42
    [b] => three						    [b] => three
    [c] => Three						    [c] => Three
)								)
--------------							--------------

--------------							--------------
arr...								arr...
Array								Array
(								(
    [a] => 2							    [a] => 2
    [b] => two							    [b] => two
    [c] => Two							    [c] => Two
)								)

key = 2								key = 2

row...								row...
stdClass Object							stdClass Object
(								(
    [a] => 2							    [a] => 2
    [b] => two							    [b] => two
    [c] => Two							    [c] => Two
)								)

results[2]...							results[2]...
stdClass Object							stdClass Object
(								(
    [a] => 2							    [a] => 2
    [b] => two							    [b] => two
    [c] => Two							    [c] => Two
)								)
--------------							--------------

$result ABOUT TO BE RETURNED...					$result ABOUT TO BE RETURNED...
Array								Array
(								(
    [42] => stdClass Object					    [42] => stdClass Object
        (							        (
            [a] => 42					      |	            [a] => 
            [b] => three				      |	            [b] => 1
            [c] => Three				      |	            [c] => 1
        )							        )

    [2] => stdClass Object					    [2] => stdClass Object
        (							        (
            [a] => 2						            [a] => 2
            [b] => two						            [b] => two
            [c] => Two						            [c] => Two
        )							        )

)								)
-------------- next part --------------
--TEST--
DB_driver::get
--SKIPIF--
<?php

/**
 * Calls the get*() methods in various ways against any DBMS.
 *
 * @see DB_Common::getAll(), DB_Common::getAssoc(), DB_Common::getCol()
 *      DB_Common::getListOf(), DB_Common::getOne(), DB_Common::getRow()
 *
 * @package  DB
 * @version  $Id: 18get.phpt,v 1.1 2004/01/31 02:35:16 danielc Exp $
 * @category Database
 * @author   Daniel Convissor <danielc at analysisandsolutions.com>
 * @internal
 */

chdir(dirname(__FILE__));
require_once './driver/skipif.inc';

?>
--FILE--
<?php

// $Id: 18get.phpt,v 1.1 2004/01/31 02:35:16 danielc Exp $

/**
 * Connect to the database and make the <kbd>phptest</kbd> table.
 */
require_once './mktable.inc';


/**
 * Local error callback handler.
 *
 * Drops the phptest table, prints out an error message and kills the
 * process.
 *
 * @param object  $o  PEAR error object automatically passed to this method
 * @return void
 * @see PEAR::setErrorHandling()
 */
function pe($o){
    global $dbh;

    $dbh->setErrorHandling(PEAR_ERROR_RETURN);
    $dbh->query('DROP TABLE phptest');

    die($o->toString());
}

$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');


$dbh->query("INSERT INTO phptest VALUES (2, 'two', 'Two', '2002-02-22')");
$dbh->query("INSERT INTO phptest VALUES (42, 'three', 'Three', '2003-03-23')");


// print phpversion() . "\n";

print "testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:\n";
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
                        false, array(100), DB_FETCHMODE_OBJECT);


$dbh->setErrorHandling(PEAR_ERROR_RETURN);
$dbh->query('DROP TABLE phptest');

?>
--EXPECT--
testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:

--------------
arr...
Array
(
    [a] => 42
    [b] => bing
    [c] => This is a test
)

key = 42

row...
stdClass Object
(
    [a] => 42
    [b] => bing
    [c] => This is a test
)

results[42]...
stdClass Object
(
    [a] => 42
    [b] => bing
    [c] => This is a test
)
--------------

--------------
arr...
Array
(
    [a] => 42
    [b] => three
    [c] => Three
)

key = 42

row...
stdClass Object
(
    [a] => 42
    [b] => three
    [c] => Three
)

results[42]...
stdClass Object
(
    [a] => 42
    [b] => three
    [c] => Three
)
--------------

--------------
arr...
Array
(
    [a] => 2
    [b] => two
    [c] => Two
)

key = 2

row...
stdClass Object
(
    [a] => 2
    [b] => two
    [c] => Two
)

results[2]...
stdClass Object
(
    [a] => 2
    [b] => two
    [c] => Two
)
--------------

$result ABOUT TO BE RETURNED...
Array
(
    [42] => stdClass Object
        (
            [a] => 42
            [b] => three
            [c] => Three
        )

    [2] => stdClass Object
        (
            [a] => 2
            [b] => two
            [c] => Two
        )

)


More information about the talk mailing list