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:johndoe@example.com, janedoe@example.com, jimdoe@example.com">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!

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.