0

I can't seemn to make this work could someone help identify what is incorrect:

$result = mysql_query("SELECT accounts_client_email.client_email FROM accounts_client_email WHERE accounts_client_email.accounts_client_id = 1", $con);

while( $row = mysql_fetch_assoc( $result)){
$new_array[] = $row; // Inside while loop
}

$tags = implode(', ', array($new_array));

echo $tags;
4
  • 3
    You are collecting $rows, not the single result column. Access the desired array field. Commented Jun 6, 2013 at 10:27
  • 1
    as mario said $new_array[] = $row['client_email']; Commented Jun 6, 2013 at 10:27
  • 1
    besides What @Dale said, $tags = implode(', ', $new_array); Commented Jun 6, 2013 at 10:28
  • I'm trying to turn a query result into a comma seperated string Commented Jun 6, 2013 at 10:30

5 Answers 5

1

You aren't adding correctly to the array, it should be:

$new_array[] = $row['client_email'];

And you are encapsulating the array into another array unnecessarily;

use:

$tags = implode(', ', $new_array);

Or echo $tags; will just output "Array"

Sign up to request clarification or add additional context in comments.

2 Comments

So my results come out like : '[email protected]','[email protected]'
OK worked it out : $new_array[] = "'".$row['client_email']."'";
1

I think you just need to use scope with your column name to populate in your array

while( $row = mysql_fetch_assoc( $result))
{
    $new_array[] = $row['client_email']; // Inside while loop
}

I would like to also to remember you that mysql_* functions are officially deprecated and no longer maintained so i would advise you to switch to mysqli or PDO for new projects.

1 Comment

@user1479981 try to use $tags = implode(', ', $new_array);
1

You need to use brackets

$new_array[] = $row[]; // Inside while loop

If this doesnt work, use

 $new_array[] = $row['client_email']; // Inside while loop

Comments

0

use for loop

$array_to_string="";
for($i=0;$i<=count($result_array)-1;$i++)
{
    if($i != count($result_array)-1)
    {
        $array_to_string .= $result_array[$i].",";
    }else
    {
        $array_to_string .= $result_array[$i];
    }
}
echo $array_to_string; // you get the string as a commoa seperated values which is available in the array

Comments

0

If you just need a comma separated string, not an array, you can do it like this:

while( $row = mysql_fetch_assoc( $result))
{
    if(empty($tags)
        $tags = $row['client_email'];
    else
        $tags .= ', '.$row['client_email'];
}
echo $tags; //will display comma separated string - no arrays needed

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.