2012-10-23 130 views
1

可能重複:
Why do I get 「Resource id #4」 when I apply print_r() to an array in PHP?獲取一個值單元格中Squl

赦免這個簡單的問題,但是我是菜鳥到SQL和PHP。簡單地說,我試圖獲得一個單元格的值,我知道它所在的列以及它所在的行,以便我可以在簡單的減法問題中使用該值。這裏是我的:

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

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

$dur = $outtime - $intime; 

使用echo它返回第一個結果爲「資源ID#4」。做一些研究我認爲這可能是因爲它向我返回了一個結果表格,而不僅僅是一個價值。值得注意的是,我試圖獲取的值是24小時格式的時間。

回答

0

使用反引號,而不是單引號逃脫列/表名:

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

另外,請不要使用mysql_*功能的新代碼。他們不再維護,社區已開始deprecation process。請參閱red box?相反,您應該瞭解prepared statements並使用PDOMySQLi。如果你不能決定,this article將有助於選擇。如果你在意學習,here is a good PDO tutorial

+0

我改變了所有的人,並沒有解決這個問題,因爲我曾以爲反引號是一樣apostropies – user1767087

+0

我,我雖然欣賞的意見我做了一些更多的研究,發現[this](http://stackoverflow.com/questions/1777801/why-do-i-get-resource-id-4-when-i-apply-print-r-to-一個陣列功能於PHP)。請注意,這些功能已過時(我上面的編輯)。 – SomeKittens

0

您需要循環結果集

$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; 
相關問題