如何使用php从列中获取精确值

时间:2013-12-11 18:39:01

标签: php mysql

我想从mysql表中的列中获取值。 mysql行的值是2个数字的简单数字。 (40)我试过了:

$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)."");

OUTPUT在这种情况下是资源ID#33

如果我这样做:

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

我得到正确的值但不是整数(简单的2位数字) 输出:stdClass Object ( [health] => 40 )

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

使用mysql_fetch_asoc()

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

$row = mysql_fetch_assoc($result);

$healthh = $row['health'];