Jump to content

Retreive float from PDO and PostgreSQL


Recommended Posts

The following returns strings instead of floats.  Am I able to retrieve floats directly from PDO and/or PostgreSQL or must I manually type cast them afterwards using PHP?  Thanks

$sql='WITH RECURSIVE t AS (bla bla bla) SELECT id, CAST(SUM(slope*value+intercept) AS FLOAT) "value", SUM(slope*prev_value+intercept)::FLOAT "prevValue" FROM t WHERE type=\'physical\' GROUP BY id';
$stmt = $this->pdo->prepare($sql);
$stmt->execute($ids);
$arr = $stmt->fetchAll(\PDO::FETCH_UNIQUE); //returns [123=>['value'=>'123.456', 'prevValue'=>'122.234'], ...]

 

Link to comment
Share on other sites

8 minutes ago, requinix said:

If you must have literal floats then yes, you have to cast them. Or instead of fighting the language you can let PHP cast them as needed.

Thanks requinix,  You mean one of these two solutions, right?  Oh, never mind, I was confused by "Or instead of fighting the language you can let PHP cast them as needed" and thought doing so was one of the two versions of casting.

Off topic.  I used to always let PHP do what it thought was right regarding casting but now am going maybe too extreme the other way.  Do you have any rules of thumb regarding type declaring primitive variables?

$string='123.4';
$float=floatval($string);
$float=(float)$string;


 

Link to comment
Share on other sites

15 minutes ago, NotionCommotion said:

Thanks requinix,  You mean one of these two solutions, right?  Oh, never mind, I was confused by "Or instead of fighting the language you can let PHP cast them as needed" and thought doing so was one of the two versions of casting.

Right: let PHP "cast", as in convert as it wants to. The loose typing system.

 

Quote

Off topic.  I used to always let PHP do what it thought was right regarding casting but now am going maybe too extreme the other way.  Do you have any rules of thumb regarding type declaring primitive variables?

There's no reason why a literal should be of the wrong type. If you need a float then write 123.4, and if you need a string that contains a float (why?) then write '123.4'.

Well, actually, with floats there are potential issues with floating-point accuracy, so there could be technical reasons for having to express a float value as a string. I don't think there are accuracy problems with literals though, but worst case an inline round() would fix that.

Besides in strict_types situations, the only time I would ever really use casts (or the function equivalents) is if I wanted to sanitize what was probably already a safe value. Like, if I had a paginate function for a SQL query, I would int-cast a hypothetical $offset argument before inserting it into the query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.