+ Start a Discussion
HarryBHarryB 

PHP toolkit: different result types on "query" command depending on record count

Hi,

I'm using the PHP toolkit (php-client-53.zip) and currently trying some queries on my SF data. Now I expected to get an array as a result every time regardless of the actual count of records found. But obviously its quite different.

Two examples to illustrate my problem (the query results are being output by PHP's print_r()):

Query 1 result (exactly one match in my SF data):

stdClass Object
(
    [type] => Contact
    [Id] => 00320000001AWQIAA4
)

Query 2 (two matches in my SF data):

Array
(
    [0] => stdClass Object
        (
            [type] => Contact
            [Id] => 00320000001AMA6AAO
        )

    [1] => stdClass Object
        (
            [type] => Contact
            [Id] => 00320000001AMAOAA4
        )
 

As you can see, the query will deliver a simple "Object" if ONE match is found, but an array of "Objects" if TWO or more matches are found. Normally one would expect it to be an array (with only one "Object") in the case of one match as well.

So, how am I supposed to walk through the result in a for- or while-loop, that will work in each case?

Any help appreciated!

-Harry

spraksprak
You could always do this:

function processObject($someObject)
{
// some stuff goes in here
}

if (is_array($result)) {
$resultSize = count($result);
for ($i = 0; $i < $resultSize; $i++) {
processObject($result[$i]);
}
} else {
processObject($result);
}
RichardSRichardS

Harry,

Three things to note on this.

1.  You may get multiple record sets - or rows.

2.  You may get an array for the ID of the object. 

3.  Each of the above may happen in the same result set.

My recomendation would be to roll your own handler that does the following - I actually like the recusive function in the previous example - this just makes it very clear for the newbie in terms of what might be happening in terms of arrays:

1.  Check to see if $result->records is an array or not, as such is_array($result->records);

     a.  If it is an array, you know you are going to iterate through and array of this formtion $result->records[$x] .  So, you can use any type of loop that suits your fancy

     b.   As you iterate, you will want to always check $result->records->Id  to see if its an array (or $result->records[$x]->Id respectively).

In line, with an emphaisis on verbositity and not brevity:

$query = "select Id, email from contact";

$result = $client->query($query,500);

if( is_array($result->records) ){

     $count = count($result->records);

     for( $x = 0 ; $x < $count ; $x++ ){

            if(  is_array( $result->records[$x]->Id ) ){

                 $ID = $result->records[$x]->Id[0];

            }else{

                 $ID = $result->records[$x]->Id;

            }

                 $email = $result->records[$x]->Email;

                //Return values if function and or process here

      }

} else {

     if(  is_array( $result->records->Id ) ){

                 $ID = $result->records->Id[0];

      }else{

                 $ID = $result->records->Id;

        }

        $email = $result->records->Email;

        //Return values if function and or process here

}

Message Edited by RichardS on 09-14-2004 05:59 AM