Hello,
I have this snippet of code to echo out the "mailer_id" with the largest value -

<?php 
$result = mysql_query('SELECT MAX(mailer_id) FROM mailer_directory')or exit(mysql_error()); 
echo 'the max mailer_id is ' . mysql_result($result, 0); ?>

My question is how do I echo out the corresponding email value for that field ?

Recommended Answers

All 6 Replies

<?php 
  $result = mysql_query('SELECT MAX(mailer_id) FROM mailer_directory')or exit(mysql_error()); 
  $row = mysql_fetch_array($result);
  echo 'the max mailer_id is ' . $row[0]; 
?>

Thanks for the input.
I should have been more concise.
Here is the SQL table structure.

CREATE TABLE `mailer_directory` (
  `mailer_id` int(10) NOT NULL auto_increment,
  `name` varchar(24) NOT NULL default '0',
  `email` varchar(44) NOT NULL default '',
  `subject` varchar(24) NOT NULL default '',
  `message` varchar(200) NOT NULL default '',
  `submitted` date NOT NULL default '0000-00-00',
  `ip` varchar(16) NOT NULL default '0',
  `submission_id` varchar(36) NOT NULL default '0',
  `ticket_id` bigint(30) NOT NULL default '0',
  PRIMARY KEY  (`mailer_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=500 ;

Essentially I want to echo out to the browser the corresponding email for SELECT MAX(mailer_id)

<?php
  $result = mysql_query('SELECT MAX(mailer_id),email FROM mailer_directory')or exit(mysql_error()); 
  $row = mysql_fetch_assoc($result);
  echo 'the max mailer_id is ' . $row['email']; 
?>

or:

<?php
  $result = mysql_query('SELECT email FROM mailer_directory ORDER BY mailer_id DESC LIMIT 1')or exit(mysql_error()); 
  $row = mysql_fetch_assoc($result);
  echo 'the max mailer_id is ' . $row['email']; 
?>
<?php
  $result = mysql_query('SELECT MAX(mailer_id),email FROM mailer_directory')or exit(mysql_error()); 
  $row = mysql_fetch_assoc($result);
  echo 'the max mailer_id is ' . $row['email']; 
?>

or:

<?php
  $result = mysql_query('SELECT email FROM mailer_directory ORDER BY mailer_id DESC LIMIT 1')or exit(mysql_error()); 
  $row = mysql_fetch_assoc($result);
  echo 'the max mailer_id is ' . $row['email']; 
?>

Hi... try to do this....

$query="SELECT mailer_id,email FROM mailer_directory where mailer_id=MAX(mailer_id)"
$result = mysql_query('')or exit(mysql_error());

Pritaeas,
Thank you - appreciated.
That does the job.
Pretty much followed your first example.
I was on the same line - just using an inappropriate 'WHERE' and using a loop.
I got a bit lost on your second example.
I'll work on that.
Cheers.

Hi... try to do this....

$query="SELECT mailer_id,email FROM mailer_directory where mailer_id=MAX(mailer_id)"
$result = mysql_query('')or exit(mysql_error());

That looks promising.
I'll have a look at that.
Thank you.

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.