DroidMatt
DroidMatt

Reputation: 183

Echo of Float value

$result = floatval(mysql_query($query))

The above is a statement to retrieve a float value from my database. When I print the value of result, it is always giving a round figure, for instance the correct value is suppose to be 3.5 but instead it prints 3. How do I fix this?

Upvotes: 0

Views: 472

Answers (1)

Corbin
Corbin

Reputation: 33447

That's actually wrong. mysql_query returns a RESOURCE (type name, not me yelling) not the actual result.

Look into the documentation on the PHP MySQL API: http://php.net/mysql

To get the actual result, you would do something like:

$q = mysql_query($query);
$val = mysql_result($q, 0);

You may also want to look into PDO/MySQLi.

If you're curious how you're actually getting a value:

When cast to a string, the resource will be something like "Resource id #3". floatval will then extract the 3.

Edit: whoops, wrong mysql link.

http://www.php.net/manual/en/ref.mysql.php is the mysql_* reference.

Upvotes: 6

Related Questions