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
  • 9,204
  • 9
  • 54
  • 124
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
  • 258,903
  • 69
  • 498
  • 492
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
  • 23,518
  • 5
  • 56
  • 58
0

Try this ::

select max(DATE(fixed_date)) as f from case_proceeding where party_id=? group by party_id 
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
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я
  • 56,591
  • 22
  • 125
  • 143