2013-12-11 44 views
0

我想從mysql表中的列中獲取值。 mysql行的值是2個數字的簡單數字。 (40)我想:如何從php欄中獲取確切的值

$healthh = intval(mysql_query("SELECT health FROM users 
      WHERE username = ".quote_smart($username)."")); 

print_r($healthh);我得到33,而不是40

也試過

$healthh = mysql_query("SELECT health FROM users 
      WHERE username = ".quote_smart($username).""); 

輸出在這種情況下,資源ID#33

如果我試試這樣:

$healthh = mysql_fetch_object(mysql_query("SELECT health FROM users 
      WHERE username = ".quote_smart($username)."")); 

我得到正確的值,但不是一個整數(簡單的2位數字) 輸出:stdClass Object ([health] => 40)

任何想法?

+0

@downvoter我可知道什麼是向下投票? – Adrian

+2

你正試圖解析結果集。您需要首先從結果集中獲取一行,然後從中獲取數據 - 「mysql_fetch_assoc」或其他類似的數據。 – andrewsi

+1

因爲你顯然是這個領域的初學者,還有2條建議:不推薦使用mysql_ *調用,不要使用任何新的開發,使用mysqli或PDO。接下來,'quote_smart'建議您嘗試以錯誤的方式解決SQL中的引用問題和安全問題。訪問[bobby-tables.com](http://bobby-tables.com/)以瞭解SQL注入,並在那裏瞭解*正確的*方法以避免引用問題和SQL注入危險。 – fvu

回答

2

使用mysql_fetch_asoc()

$result = mysql_query("SELECT health FROM users 
     WHERE username = ".quote_smart($username).""); 

$row = mysql_fetch_assoc($result); 

$healthh = $row['health']; 
相關問題