I am new to PHP and I have a problem in my code. I have two tables:
seeker
seeker_nic | username
-----------+----------
111 | ali
222 | umer
333 | raza
bloodrequest
id | seeker_nic | requireddate
---+------------+--------------
1 | 111 | 2012/9/9
2 | 222 | 2012/5/8
3 | 111 | 2012/10/11
4 | 111 | 2012/11/12
5 | 222 | 2012/7/9
6 | 333 | 2012/4/4
Now I want to list users one time with maximum date like..
s.no | username | requireddate
-----+----------+--------------
1 | ali | 2012/11/12
2 | umer | 2012/7/9
3 | raza | 2012/4/4
I am using this query...
select seeker.username, max(bloodrequest.requireddate)
from seeker
join bloodrequest on seeker.seeker_nic=bloodrequest.seeker_nic
group by seeker.username
This query works in phpMyAdmin, it shows the result that I wanted. But when I run this query in PHP an error occured on requireddate column:
"Undefined index: requireddate in C:\wamp\www\list.php on line 64"
Line 64 is:
<td><?php echo $rec['requireddate']; ?></td>
Can anyone please tell me where is the problem
select seeker.username, max(bloodrequest.requireddate) as requireddate
from seeker
join bloodrequest on seeker.seeker_nic=bloodrequest.seeker_nic
group by seeker.username
should work then. you need to give the column the name you want.
max(bloodrequest.requireddate)
would be the column name as that is what you are selecting. by using as requireddate
, you are re-naming that to requireddate
so it will be $rec['requireddate']
when you are in php - Jonathan Kuhn 2012-04-04 19:58