1

I want to get or fetch maximum date from a table grouped by party_id.

To do this I tried:

$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding group by party_id where party_id='$row[party_id]'"));

After that, I want to print that maximum date like this:

echo $check['f'];

but it does not work. I think there is a mistake in the mysql_query.

pjmorse
  • 8,994
  • 9
  • 54
  • 119
user1814328
  • 41
  • 1
  • 6
  • 1
    group by goes after where. instead of putting it on on one line, breaking it down so you can use error checking is advised –  Nov 28 '12 at 06:21
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Nov 28 '12 at 06:29

4 Answers4

0

try, GROUP BY (which should come after WHERE clause) isn't needed here since you already filtered from WHERE

$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding where party_id='$row[$party_id]'"));

but your quesry is vulnerable with SQL Injection, please read the article below to protect against it

Community
  • 1
  • 1
John Woo
  • 249,283
  • 65
  • 481
  • 481
0

try this:

GROUP BY clause is not required here

select max(fixed_date) as f 
from   case_proceeding 
where  party_id='$row[party_id]
Joe G Joseph
  • 22,627
  • 4
  • 51
  • 56
0

Try this ::

select max(DATE(fixed_date)) as f from case_proceeding where party_id=? group by party_id 
Sashi Kant
  • 12,829
  • 9
  • 40
  • 65
0

try

$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding where party_id='$row[party_id]' "));

Note :mysql_* function are deprecated so try pdo or mysqli

NullPoiиteя
  • 55,099
  • 22
  • 123
  • 139