Jump to content

[SOLVED] Print Array Separated By Comma (Probably Easy)


hoopplaya4

Recommended Posts

Hey All,

 

I've got a question, that's probably easy, but for some reason, I can't figure it out.

 

I have a row in my database that stores user emails.  I would like to pull the entire array of emails from the database, and place it into a hyperlink.  For example:

 

<a href="mailto:[email protected], [email protected], [email protected]">Email Entire Database</a>

 

However, I'm just not quite sure how to do so.  So far, I have:

 

<?php

$sql = "SELECT usrID, usrEmail, usrFirst, usrLast";
      $sql .= " FROM tblUsers";

	require("../connection.php");

   		$rs=mysql_db_query($DBname,$sql,$link);

    	if ($rs) {
     	$row=mysql_fetch_array($rs)
?>
	<a href='mailto:<?php echo $row["usrEmail"]; ?>'>

But this only displaying the first email.  How might I print all emails, separated by a comma?  I'm sure this is easy, but I'm not sure how to do it!

 

Thanks!

$user='username';

$hostmachine='localhost';

$password='password';

$database='dbname';

mysql_connect($hostmachine,$user,$password);

mysql_select_db($database);

$query = "SELECT * FROM table WHERE field like 'whatever'";

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result)){

          do something here with $row[0],$row[1], etc...

          }

 

 

This will loop through each result line.

Thanks for the reply.  I sort of understand the "$row[0],$row[1], etc..." but, the name of my column is usrEmail, so would the correct syntax be: $row['usrEmail'][0], or what?

 

Also, the number of results in the table is always changing.  For example, I may have 30 emails one day, but 35 the next.

 

So how would I go about it so I don't have to count up using [0],[1],[2], etc?

 

Thanks.

<?php
$user='username';
$hostmachine='localhost';
$password='password';
$database='dbname';
mysql_connect($hostmachine,$user,$password);
mysql_select_db($database);
    $sql = "SELECT usrID, usrEmail, usrFirst, usrLast";
    $sql .= " FROM tblUsers";
   $result = mysql_query($sql) or die(mysql_error());
    $emails = array();
   while ($row = mysql_fetch_assoc($result)){
         $emails[] = $row['usrEmail'];
   }
   $emails = implode(", ", $emails);

   echo '<a href="mailto: ' . $emails . '">Email Them</a>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.