I have problem with SQL query.
I have table in my database which stores attendance of students.
It has fields Teacher_Id, Roll_No, Date, Present('0' if student is absent on that date and '1' if present).
I wrote following query

SELECT count(Present ) , sum( Present ) FROM attendance_info WHERE Teacher_Id ='12' AND Roll_No = 2 AND Date BETWEEN '2011-03-09' AND '2011-03-15'

It works fine when I run it in query analyzer but when i integrated it in PHP code, it didn't gave any results for sum and count was 0.

Is there any way to store count and sum in PHP variables?

Plz do answer.

I'm not a PHP guy, but I do have a suggestion.

Many programming languages/environments put your SQL through a grinder of sorts (called a "driver") before you can get to the target database. In many cases, when you use "reserved words" as column names, it can create issues. You may want to try putting brackets around "date" and change the format of the date like so:

SELECT count(Present), sum(Present) 
FROM attendance_info 
WHERE Teacher_Id ='12' 
AND Roll_No = 2 
AND [Date] BETWEEN '03/09/2011' AND '03/15/2011'

It might make no difference (like I said, I'm not a PHP guy) but it might be worth a try. If it doesn't work, at least it's one more thing you've ruled out.

I had another thought...perhaps you could also try naming the aggregate columns, like so:

SELECT count(Present) as PresCount, sum(Present) as PresSum

Maybe that would give them visible names for PHP to grab onto?

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.