6

I have an array with 30k items, and implode returns nothing. No error message, no memory problems, just nothing.

If I use array_slice and slice the array to 100 items, it works fine. It also works for 7k array, but not for this one.

However, in another topic I found this code, which works just fine:

$arr = array();
for ($i = 0; $i < 50000; $i++) {
$arr[] = str_shuffle('This sentance is of average length, which The Internet says is  aboout 14.2 words.'); 
}
echo implode(PHP_EOL, $arr);

But with PHP_EOL I can't use that in my select, the string needs to be seperated by ','.

So I have two questions: is there any way to make this work and how can I catch this error? Because testing the implode output does not work, is_null, strlen, is_string, empty, isset, all these tests fail.

Community
  • 1
  • 1
Honza
  • 323
  • 3
  • 14
  • 1
    Is there anything in your error logs? – Dave Child Feb 25 '14 at 13:13
  • 2
    "*i cant use that in my select*". Why? What happens? Does it pop up with an error? Blank page? Does your browser shut down? Does your server start burning? Does the Apache service halt? Does it start sending spam? That said, you shouldn't be doing this. You're generating *at least* 3.91 MB (`(50000 * 82) / 1024 / 1024`) more content for every single request. – h2ooooooo Feb 25 '14 at 13:14
  • has for example item nr. 7k1 an qoute in it's string? is everything urlencoded? addslashes() for example... or does it break the array! – Nijboer IT Feb 25 '14 at 13:18
  • try `"
    "` instead of PHP_EOL
    – krishna Feb 25 '14 at 13:18
  • Is your array multidimensional? If it is implode wont work – Dale Feb 25 '14 at 13:19
  • what about server timeout.. whats you're script running time! – Nijboer IT Feb 25 '14 at 13:20
  • Hello, i just test this on my server an looks working fine. So, do you enabled error reporting on your server ? – Nitin Kumar Soni Feb 25 '14 at 13:21
  • @h2ooooooo because this doesnt work: SELECT * FROM table WHERE (productID IN (30 31 32 33 etc); – Honza Feb 25 '14 at 13:21
  • 1
    @user3351349 You're missing some commas? – h2ooooooo Feb 25 '14 at 13:24
  • @TD_Nijboer The array is fine, its just ids from database, nothing wrong there – Honza Feb 25 '14 at 13:27
  • @DaveChild I found this error in the log [24-Feb-2014 12:58:09] PHP Catchable fatal error: Object of class someClassName could not be converted to string in C:\***\***\***\***\***\***\someClassName.php on line 273 – Honza Feb 25 '14 at 13:29
  • @user3351349 *What's* on line 273 of `someClassName.php`? – h2ooooooo Feb 25 '14 at 13:30
  • @h2ooooooo $p_ids = implode(',', $available_brands_array); – Honza Feb 25 '14 at 13:31
  • 2
    @user3351349 One of your values in `$available_brands_array` isn't a string - it's a class of the type `someClassName`. Use `print_r` and figure out which, or use the following: http://pastebin.com/XJHg8dpH – h2ooooooo Feb 25 '14 at 13:34
  • @Dale It is not multiarray. TD_Nijboer I dont think its a timeout problem, its not like that implode takes 2 minutes to return a blank pages, it takes like 1 sec. – Honza Feb 25 '14 at 13:36
  • @h2ooooooo That script does not print anything, all values are string. It just simple IDs from db, [30] => 30 [31] => 31 [32] => 32 [33] => 33 – Honza Feb 25 '14 at 13:48
  • Why bother using implode. Pass the array in directly as a parametrized query. You should not be passing in your SQL parameters as text, not best practices. http://www.php.net/manual/en/pdo.prepare.php – ladieu Feb 25 '14 at 15:49
  • Ok so maybe I spoke too soon. In doctrine 2 you can pass in an array.. I'm not sure PDO supports this. So I apologize for that. Was an idea to look at though – ladieu Feb 25 '14 at 15:53
  • Here is a thread for you to look at, there might be some solutions for you here: http://stackoverflow.com/questions/920353/php-pdo-can-i-bind-an-array-to-an-in-condition – ladieu Feb 25 '14 at 15:54
  • Here is another: http://stackoverflow.com/questions/15866640/php-pdo-not-taking-imploded-array-as-question-mark-parameter-for-in-clause-in Basically if you make X question marks (using a loop or whatever) you can pass the array in as a parameter. Kind of ghetto. Probably better off just using a real database framework like Doctrine 2 – ladieu Feb 25 '14 at 15:58
  • Here you go bro: '('.implode(',',array_fill(0,sizeof($this->Values),'?')).')'; Dug that up in some old code that solves this exact problem. That fills err up w/ ? marks – ladieu Feb 25 '14 at 15:58

1 Answers1

1

EDIT: Facepalm moment after writing this answer that adding a for loop to make ? marks doesn't seem any better than just using it to output the data. Anyway I suppose you could try

<?
 $questionMarks =implode(',',array_fill(0,sizeof($myarray),'?'));
?>

and see if that has more luck for you.

You can use parametrized queries to circumvent your issue.

<?php

$db = new PDO(...);

//$myarray is some random sized php array of potential myid values
$questionMarks='';

//check to see if runtime is acceptible for your applicaition
for ($i = 0; sizeof($myarray); $i++)
    $questionMarks=",?";
$questionMarks=substr($questionMarks,1,strlen($questionMarks)-1);

/* you could try implode(',',array_fill(0,sizeof($myarray),'?')) but as you said implode might not work */

$sql = 'select myfield from mytable where myid in ('.$questionMarks.')';
$sth = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute($myarray);
if (!$sth) {
    echo "\nPDO::errorInfo():\n";
    print_r($db->errorInfo());

}  


while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
   echo $row['myfield']  . "<br />";
}
?>
ladieu
  • 127
  • 5
  • 17
  • Also check for database errors.. you might be creating invalid SQL via some data in your array that is not correct. – ladieu Feb 25 '14 at 16:10