dumping contents of MySQL table into PHP mail()

A

Anonymous

Guest
Kind of a general question....

I have a MySQL table, and basically what I want to do is dump the full contents of the table into the mail() function and email it to myself....In the email, I need the values of a given row of the table grouped together (not the columns), starting from the top row of the table and moving down....I can't seem to figure out an easy way to do this, without doing a million database queries...

Thanks for any help.
 
is this what you want?

<?php
mysql_connect('localhost','username','password');
mysql_select_db('database_name') or die(mysql_error());

//Table you want info from
$table = 'Insert_Table_Name';

//To address
$address = 'INSERT_EMAIL_ADDRESS';
//subject
$subject = 'My tables';
//beginning of message
$message = 'Table: ' . $table . "\n // ";
//From address
$from = 'myself@' . $_SERVER['SERVER_NAME'];

$query = "SELECT * FROM $table";
$result = mysql_query($query);

//Returns number of columns
$cols = mysql_num_fields($result);

for ($i=0; $i<$cols; $i++)
$message .= mysql_field_name($result, $i) . ' // ';

while ($row = mysql_fetch_array($result)) {
$message .= "\n";
for ($i=0; $i<=$cols; $i++)
$message .= ' // ' . $row[$i];
}
mail($address, $subject, $message, 'From: ' . $from);
?>

I hope you know what to change in the script though. The bold is what you probably have to change.
 
Back
Top