1

Possible Duplicate:
Why do I get “Resource id #4” when I apply print_r() to an array in PHP?

Pardon this simple question, however I am a rookie to SQL and PHP. Simply enough I am trying to get the value of a cell that I know the column it is in as well as the row it's in so I can use that value in a simple subtraction problem. Here is what I have:

$outtime = mysql_query("SELECT `Event1CheckOut1`
    FROM attendance
    WHERE ID = '1234'");

$intime = mysql_query("SELECT `Event1CheckIn1`
    FROM attendance
    WHERE ID = '1234'");

$dur = $outtime - $intime;

Using echo it returned the result for the first one as "Resource id #4". Doing some research I think this may be because it is returning to me a table of the results not simply a value. It may be worth noting that the values I am trying to fetch are times in a 24 hour format.

Community
  • 1
  • 1

2 Answers2

0

Use backticks instead of single quotes to escape column/table names:

SELECT `Event1CheckIn1` FROM `attendance` WHERE ID='1234'

Also, please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is a good PDO tutorial.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • I changed all of them and it didn't fix the problem, I appreciate the advice though because I had assumed backticks to be the same as apostropies – user1767087 Oct 23 '12 at 02:56
  • I've done some more research and found [this](http://stackoverflow.com/questions/1777801/why-do-i-get-resource-id-4-when-i-apply-print-r-to-an-array-in-php). Note that these functions are obsolete (my edit above). – SomeKittens Oct 23 '12 at 03:03
0

You need to loop over the result set

$outtimeR = mysql_query("SELECT `Event1CheckOut1`
    FROM attendance
    WHERE ID = '1234'");
while($row = mysql_fetch_assoc($outtimeR )){
  $outtime = $row['Event1CheckOut1']; 
}

$intimeR = mysql_query("SELECT `Event1CheckIn1`
    FROM attendance
    WHERE ID = '1234'");

while($row = mysql_fetch_assoc($intimeR )){
  $intime = $row['Event1CheckIn1']; 
}

$dur = $outtime - $intime;
case1352
  • 1,126
  • 1
  • 13
  • 22