Jump to content


Photo

mysql query 'GROUP_CONCAT' - need help


Best Answer Psycho, 24 March 2013 - 12:26 PM

$query = "SELECT id FROM table ORDER BY id DESC";
$result = mysql_query($query) or die ("no query");

$idsArray = array();
while($row = mysql_fetch_assoc($result))
{
    //Add each ID as a new element in the array
    $idsArray[] = $row['id'];
}

//Output the array to verify
echo "<pre>" . print_r($idsArray, 1) . "</pre>";
Go to the full post


  • Please log in to reply
7 replies to this topic

#1 carlosp

carlosp

    Member

  • Members
  • PipPip
  • 12 posts

Posted 23 March 2013 - 09:14 PM

here's what i want to do:

i have a table which has id's and i want to select them in one query, then put them in an array...

 

table example:

id                  name          surname

1                  john              doe

2                 johnny          tacev

.

.

19384          travis          malega

 

i tried this:

$query = "SELECT GROUP_CONCAT(id) AS id_coll FROM table ORDER BY id DESC";

$result = mysql_query($query) or die ("no query");

while($row = mysql_fetch_assoc($result))
{    $cs = implode(", ", $row);

}

 

it works but only for the first 256 rows... i can't change server settings...

 

please help me with this

 

thanks!

 

PS: i hope i posted in the right section



#2 carlosp

carlosp

    Member

  • Members
  • PipPip
  • 12 posts

Posted 23 March 2013 - 09:16 PM

here's what i want to do:

i have a table which has id's and i want to select them in one query, then put them in an array...

 

table example:

id                  name          surname

1                  john              doe

2                 johnny          tacev

.

.

19384          travis          malega

 

i tried this:

$query = "SELECT GROUP_CONCAT(id) AS id_coll FROM table ORDER BY id DESC";

$result = mysql_query($query) or die ("no query");

while($row = mysql_fetch_assoc($result))
{    $cs = implode(", ", $row);

}

 

it works but only for the first 256 rows... i can't change server settings...

 

please help me with this, it doesn't matter if i need to change the query in any other way. i just need to have the id's in an array (1,2,3,......,19384)

 

thanks!

 

PS: i hope i posted in the right section



#3 haku

haku

    Advanced Member

  • Staff Alumni
  • 6,178 posts

Posted 23 March 2013 - 10:26 PM

You don't need GROUP_CONCAT for this. Just select ID normally, then loop through the results and put them into an array.

#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,965 posts
  • LocationCanada

Posted 23 March 2013 - 10:31 PM

But, the reason you are having a problem is that you are using GROUP_CONCAT() but you don't have a GROUP BY to group on.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,789 posts
  • LocationCheshire, UK

Posted 24 March 2013 - 12:50 AM

By row 256 you have probably hit the 1000 char size limit


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#6 carlosp

carlosp

    Member

  • Members
  • PipPip
  • 12 posts

Posted 24 March 2013 - 04:56 AM

thank you all for replying, could you please give me an example of selecting all the id's and put them into an array?

one which works, because i couldn't build one until now....



#7 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,965 posts
  • LocationCanada

Posted 24 March 2013 - 12:26 PM   Best Answer

$query = "SELECT id FROM table ORDER BY id DESC";
$result = mysql_query($query) or die ("no query");

$idsArray = array();
while($row = mysql_fetch_assoc($result))
{
    //Add each ID as a new element in the array
    $idsArray[] = $row['id'];
}

//Output the array to verify
echo "<pre>" . print_r($idsArray, 1) . "</pre>";

Edited by Psycho, 24 March 2013 - 12:27 PM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#8 carlosp

carlosp

    Member

  • Members
  • PipPip
  • 12 posts

Posted 24 March 2013 - 01:58 PM

thanks for your reply's

just fixed this morning the query...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com