Jump to content

Select highest value from column in database table


ecopetition

Recommended Posts

Thanks but then from the code:

 

{
	$sql = "SELECT MAX(post_id)
	  	  FROM posts"
	          ;
		  $result = $db->query($sql);
		  while($row = $db->fetch_assoc($result))
		  {
		  	$highest_post[] = $row;
		  }
  }

 

"$highest_post" is an array rather than a number. Any ideas what's wrong?

 

Thanks.

Thanks but then from the code:

 

{
	$sql = "SELECT MAX(post_id)
	  	  FROM posts"
	          ;
		  $result = $db->query($sql);
		  while($row = $db->fetch_assoc($result))
		  {
		  	$highest_post[] = $row;
		  }
  }

 

"$highest_post" is an array rather than a number. Any ideas what's wrong?

 

Thanks.

 

Remove the [] in $highest_post, 8th line.

Remember that mysql_fetch_* always returns an array of results. You need to target the specific result you are after:

 

<?php
$sql = mysql_query("SELECT MAX(col) AS max FROM my_table");

// Either get it directly like this:
$val = mysql_result($sql, 0, 'max');

// Or, fetch and get the array val:
$res = mysql_fetch_assoc($sql);
$val = $res['max'];
?>

 

Good luck.

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.