2

Can anyone tell me what's wrong with my code? I get the error message "Warning: join() [function.join]: Invalid arguments passed in..." on the very last line - echo join( $URL, '
' );

I checked this discussion and this one, but nothing clicks. I pasted my query into phpMyAdmin > SQL, and it works perfectly, returning a table with two columns listing values in the field URL that have multiple instances along with the number of instances (e.g. Bill_Gates | 4).

So it looks like there must be a problem with my code - unless there's a bug in a file higher up the food chain, but I don't think that's likely.

$stm = $pdo->prepare("select URL, count(*)
from ((SELECT 'GZ' AS GSiteID, NULL as Site, 'Life' AS GSection, GZL.Taxon AS URL
       FROM gz_life GZL WHERE GZL.Taxon = :MyURL
      ) UNION ALL
      (SELECT 'All' AS GSiteID, NULL as Site, 'World' AS GSection, GG.Name AS URL
       FROM gw_geog GG WHERE GG.Name = :MyURL
      ) UNION ALL
      (SELECT 'PX' AS GSiteID, Site, 'People' AS GSection, Ppl.URL
       FROM people Ppl WHERE Ppl.URL = :MyURL
      )
     ) t
group by URL
having count(*) > 1;");
 $stm->execute(array(
 'MyURL'=>$MyURL
 ));

while ($row = $stm->fetch())
{
 $URL = $row['URL'];
}

echo join( $URL, '<br>' );

P.S. I posted var_dump($URL); at the very end of the above script, but it only displays string(9) "Zachaenus", which doesn't make any sense to me. (I think Zachaenus is a scientific name from the table Life.) But I've never used var_dump before so maybe I'm not doing it correctly.

2 Answers 2

2

You need to make use of the glue parameter first.

echo join( '<br>',$URL );
           ^^^^^  ^^^^   //<---- Order Interchanged 

Alternatively, you could make use of implode which does the same of the join.

Secondly.. the $URL must be array.

$URL = array();
while ($row = $stm->fetch())
{
 array_push($URL,$row['URL']);
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's weird; I get the same results. I normally use echo join after an ARRAY, like this... while ($row = $stm->fetch()) { $URL[] = $row['URL']; } But the array brackets - [] - apparently don't work with this kind of query. I'll do some research on implode and see if that works. Thanks.
The $URL must be an array for the join or implode to work. Have you modified $URL inside your while loop as $URL[] ?
Yes, I've tried it with and without the brackets, using both echo join and implode. But let me run through my tests again, to make sure I didn't do something wrong...
Yes, that works... $URL = array(); while ($row = $stm->fetch()) { array_push($URL,$row['URL']); } echo join('<br>', $URL);
1

My guess is join is looking for a array and not a string which you are giving it and you got the glue and pieces flipped so try the following

join is an alias of implode which is looking for join|implode( string $glue , array $pieces )

while ($row = $stm->fetch()){
    $URL[] = $row['URL'];
       #^^
}

echo join('<br>', $URL);

$URL[] this will give you a array and not a string which is being replaced every time the while loop executes and only giving you the last element from $row.

According to this Problem with: Fatal error: [] operator not supported for strings it says something like you might have initialized $URL to a string or some other type other than a array and need to change it to $URL = array(); or use a different variable name.

1 Comment

Thanks, but that gives me the error message "Fatal error: [] operator not supported for strings in..." on the line $URL[] = $row['URL'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.