NotionCommotion Posted July 7, 2020 Share Posted July 7, 2020 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'], ...] Quote Link to comment https://forums.phpfreaks.com/topic/311044-retreive-float-from-pdo-and-postgresql/ Share on other sites More sharing options...
requinix Posted July 7, 2020 Share Posted July 7, 2020 Actually PDO as a whole doesn't support floats. 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. Quote Link to comment https://forums.phpfreaks.com/topic/311044-retreive-float-from-pdo-and-postgresql/#findComment-1579419 Share on other sites More sharing options...
NotionCommotion Posted July 7, 2020 Author Share Posted July 7, 2020 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; Quote Link to comment https://forums.phpfreaks.com/topic/311044-retreive-float-from-pdo-and-postgresql/#findComment-1579421 Share on other sites More sharing options...
requinix Posted July 7, 2020 Share Posted July 7, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311044-retreive-float-from-pdo-and-postgresql/#findComment-1579423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.