Member Avatar for jpknoob

Hi all, i have a piece of code that selects some data from a database and displays the results in a table. I am trying to edit the code to allow me to email the table to a an address entered, but am having difficulties, can anyone offer any advice?

This is my code that i am trying to fix;

if(isset($_POST['send']))	{
	
	$subject = "Data";
			
	$query = "SELECT * From database
				ORDER by columnA";
				
	$result = mysql_query($query) or die(mysql_error());
				
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))	{
	
		$A = $row['A'];
		$B = $row['B'];
		$C = $row['C'];
	
		$body = "";
	
	}	
	
	$email = $_POST['email'];
	
	if (mail($email, $subject, $body)) {
		echo("<p>successfully sent</p>");
	} 
	
	else {
		echo("<p>delivery failed...</p>");
	}
	
}

I tried to write out the table code in $body, but don't get the desired results. Any help would be greatly appreciated.

See example #4 in the manual.

<?php
if(isset($_POST['send']))	{
    //Connection
    $email = $_POST['email'];
    $query = "SELECT * From database	ORDER by columna";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result))	{ 		
        $A = $row['a'];		
        $B = $row['b'];		
        $C = $row['c']; 
    }	

    $to = $email;
    $subject = "Data";
    $message = "$A\n $B\n $C\n";
    $from = "someonelse@example.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
}
?>

if your still having problems emailing, then you need to configure your email
http://www.w3schools.com/php/php_ref_mail.asp

Member Avatar for jpknoob

Thanks for the quick responses.

I have followed the link provided by priteas, this seems to be what I'm after, however, i only seem to get one result rather than the whole table, which is 130 items.

Try this

$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th>
    </tr>';

    //Connection    
    $query = "SELECT * From database	ORDER by columna";
    result = mysql_query($query) or die(mysql_error());    
    while ($row = mysql_fetch_array($result))	{
    $message = '
    <tr>
      <td>".$row['a']."</td><td>".$row['b']."</td><td>".$row['c']."</td>
    </tr>';
    }
$message = '
 </table>
</body>
</html>
';
Member Avatar for jpknoob

Unfortunately that didn't work. I tried something similar, but am now running out of ideas. Before i was getting one result and now I'm getting blank emails. my updated code;

if(isset($_POST['send']))	{
			
			$subject = "Alexa Ranking";
			$email = $_POST['email'];
			
			// To send HTML mail, the Content-type header must be set
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			
			$message = '<html>
			<head>
			<title>Alexa Rankings</title>
			</head>
			<body>
			<table>
			<tr>
			<th>Blog</th>
			<th>Site</th>
			<th>Rank</th>
			</tr>';
			
			$query = "SELECT * From alexaRanking
						ORDER by rank";
						
			$result = mysql_query($query) or die(mysql_error());
			
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC))	{
			
				$blog = $row['blogs'];
				$site = $row['site'];
				$rank = $row['rank'];
			
				$message = '
				<tr>
					<td>$blog</td>
					<td>$site</td>
					<td>$rank</td>
				</tr>';
			}	
			
			$message = '
				</table>
				</body>
				</html>';
			
			if (mail($email, $subject, $message, $headers)) {
				echo("<p>successfully sent</p>");
			} 
			
			else {
				echo("<p>delivery failed...</p>");
			}
			
		}

I noticed that when they have mulitple variables that are being entered, the second variable has a . in front of the variable.
so
$message = ''
$message .='they the second one'

Try this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))	{ 			
    $blog = $row['blogs'];				
    $site = $row['site'];				
    $rank = $row['rank']; 				
    $message .= '
    <tr>					
        <td>$blog</td>
        <td>$site</td>					
        <td>$rank</td>				
    </tr>';			
}	 			
$message .= '				
</table>				
</body>				
</html>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.