NYCPHP Meetup

NYPHP.org

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

Daniel Convissor danielc at analysisandsolutions.com
Mon Feb 2 15:16:34 EST 2004


Hi Adam:

On Mon, Feb 02, 2004 at 09:01:53AM -0500, Adam Maccabee Trachtenberg wrote:
>
> It took me a while to get the whole testing framework up and running,

Sorry about that.  Thanks for your perseverance.


> but now that I have, your script seems to cause a core dump.

Interesting.


> I have a backtrace, but it would be nicer to see if we can get this
> down to a shorter script that doesn't involve lots of includes and
> database accesses.

Done.  I've pared down the PEAR.  Still produces weird output, but not the 
same weird output.  Also, the output changes if the SQLite file is 
unlinked at the end!  Wacky!

THANKS!

--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 --------------
--TEST--
PEAR DB getAssoc Messed Up Under PHP 5
--FILE--
<?php

/*
 * PHP 5 acts weirdly when using PEAR DB's getAssoc() method
 * when returning objects.  Works fine under PHP 4.
 *
 * PHP snapshot used:  php5-win32-200402011930
 *
 * This test uses an SQLite for the database.  Make sure to set the
 * $database variable which is right after this comment.
 *
 * Another weird thing is if the database file is unlinked at the end,
 * the output changes!  Toggle $unlink between 0 and 1 to see what I mean.
 *
 * Based on pear/DB/tests/18get.phpt
 * "testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:"
 */


$database = 'd:/sqlite/stp';
$unlink   = 0;


// DECLARE EMULATIONS OF THE PEAR DB FUNCTIONS =======

function &getAssocTest($query, $conn) {

    $res = sqlite_query($query, $conn);

    $results = array();

    while ($row = fetchRow_and_fetchInto($res)) {
        $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);

        $results[$key] = $row;

        print "\nresults[$key]...\n";
        print_r($results[$key]);

        print "--------------\n";
    }

    echo "\n\$result ABOUT TO BE RETURNED...\n";
    print_r($results);

    return $results;
}


function &fetchRow_and_fetchInto($result) {
    $arr = sqlite_fetch_array($result, SQLITE_ASSOC);

    if (!$arr) {
        $tmp = null;
        return $tmp;
    }

    $arr = (object) $arr;
    return $arr;
}


$conn = sqlite_open($database);


// SET UP THE TEST TABLE AND VALUES ==================

$query = "
    CREATE TABLE phptest (
      a INTEGER NULL,
      b CHAR(40) DEFAULT 'def' NOT NULL,
      c VARCHAR(255) NULL,
      d VARCHAR(20) NULL)
";

ini_set('track_errors', true);
$php_errormsg = '';

$res = @sqlite_query($query, $conn);
if ($res === false) {
    if ($php_errormsg == 'table phptest already exists') {
        // no sweat.
    } else {
        echo "$php_errormsg";
    }
}

$res = sqlite_query("INSERT INTO phptest
        VALUES (42, 'bing', 'This is a test', '1999-11-21')", $conn);
$res = sqlite_query("INSERT INTO phptest
        VALUES (2, 'two', 'Two', '2002-02-22')", $conn);
$res = sqlite_query("INSERT INTO phptest
        VALUES (42, 'three', 'Three', '2003-03-23')", $conn);


// CALL THE FUNCTION =================================

$res = getAssocTest('SELECT a, b, c FROM phptest ORDER BY b', $conn);


// DROP THE DATABASE =================================

$res = sqlite_query('DROP TABLE phptest', $conn);
sqlite_close($conn);

if ($unlink) {
    unlink($database);
}

?>
--EXPECT--
--------------
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