Results 1 to 8 of 8

Thread: implode a database row to CSV string

  1. #1
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default implode a database row to CSV string

    I have been trying this for a couple of hours now but i cant get it to work: Getting the Invalid amount of parameter error.

    Here is what i have so far:

    Code:
    SELECT administrator_adm.email_adm,administrator_adm.fname_adm, 
    administrator_adm.lname_adm, administrator_adm.level_adm,                       
    administrator_adm.archive_adm, staff_stf.isemployed_stf 
    FROM (administrator_adm LEFT JOIN staff_stf ON 
    staff_stf.idadm_stf=administrator_adm.id_adm) 
    WHERE administrator_adm.level_adm='teacher' 
    AND administrator_adm.archive_adm is null AND 
    staff_stf.isemployed_stf=1 ";
    
    $res_group = mysql_query($sel_group) or 
    die("the sel_group query complained: ".mysql_error());
    
    
    
     $row_group = mysql_fetch_assoc($res_group);
     $show = implode(",", $row_group['email']);
    The query work over all its just the implode. I want to take all the email address from the query and convert them to a comma seperated string so i can pass them to a $to vatiable to send email to multiple addresses. I could have just looped the whole send email block but i consider this less efficient. Any ideas what i am doing wrong here
    Last edited by leocrawf; Sep 25, 2009 at 12:20 PM.
    "...men are that they might have joy."
    Nephi

  2. #2
    Join Date
    Dec 2004
    Posts
    1,181
    Rep Power
    0

    Default

    Quote Originally Posted by leocrawf View Post
    I have been trying this for a couple of hours now but i cant get it to work: Getting the Invalid amount of parameter error.

    Here is what i have so far:

    Code:
    SELECT administrator_adm.email_adm,administrator_adm.fname_adm, 
    administrator_adm.lname_adm, administrator_adm.level_adm,                       
    administrator_adm.archive_adm, staff_stf.isemployed_stf 
    FROM (administrator_adm LEFT JOIN staff_stf ON 
    staff_stf.idadm_stf=administrator_adm.id_adm) 
    WHERE administrator_adm.level_adm='teacher' 
    AND administrator_adm.archive_adm is null AND 
    staff_stf.isemployed_stf=1 ";
    
    $res_group = mysql_query($sel_group) or 
    die("the sel_group query complained: ".mysql_error());
    
    
    
     $row_group = mysql_fetch_assoc($res_group);
     $show = implode(",", $row_group['email']);
    The query work over all its just the implode. I want to take all the email address from the query and convert them to a comma seperated string so i can pass them to a $to vatiable to send email to multiple addresses. I could have just looped the whole send email block but i consider this less efficient. Any ideas what i am doing wrong here

    You are using $row_group['email'] even though the column you are interested in is called 'email_adm'. That may be your problem.
    'If we're supposed to work in Hex, why have we only got A fingers?'

    Follow Me: @psybuck2002us

  3. #3
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Quote Originally Posted by psybuck2002us View Post
    You are using $row_group['email'] even though the column you are interested in is called 'email_adm'. That may be your problem.
    o, ok. I fixed the error but i am still getting BAD ARGUMENTS error!
    "...men are that they might have joy."
    Nephi

  4. #4
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    implode takes an array, but the second parameter you passed is not an array.

    quite simply, you know how to do it correctly (as you have described to be inefficient) but have chosen to write code which is incorrect.

    you have to iterate the rows and put the email in an array if you want to use implode.

    i am not sure if there is an existing php function to do this.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  5. #5
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
    SELECT administrator_adm.email_adm,administrator_adm.fname_adm, 
    administrator_adm.lname_adm, administrator_adm.level_adm,                       
    administrator_adm.archive_adm, staff_stf.isemployed_stf 
    FROM (administrator_adm LEFT JOIN staff_stf ON 
    staff_stf.idadm_stf=administrator_adm.id_adm) 
    WHERE administrator_adm.level_adm='teacher' 
    AND administrator_adm.archive_adm is null AND 
    staff_stf.isemployed_stf=1 ";
    
    $res_group = mysql_query($sel_group) or 
    die("the sel_group query complained: ".mysql_error());
    
    $row_group = array();
    
    while ($row = mysql_fetch_assoc($result)) $row_group[] = $row['email'];
    
    $show = implode(",", $row_group['email']);
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  6. #6
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Thanks for your effort, i tried it and it did not work however. I am getting the same bad arguments error..
    "...men are that they might have joy."
    Nephi

  7. #7
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    The last line of icymint3's codde should be ;

    $show = implode(",", $row_group );

    the second parameter MUST be an array. Not just any array but one which returns true when you do a if is_array($variable) on it.


    if you are working with files try this;
    http://jm2.php.net/manual/en/function.fputcsv.php

  8. #8
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    thanx man, its all the copy and paste that caused it

    i tried to get my php environment up... but it was taking too long so i just abandoned it...
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •