note 124111 added to pdostatement.getcolumnmeta

From: Date: Tue, 13 Aug 2019 10:05:35 +0000
Subject: note 124111 added to pdostatement.getcolumnmeta
Groups: php.notes 
Request: Send a blank email to php-notes+get-211883@lists.php.net to get a copy of this message
I was able to successfully pull off basic type conversion for integer and float values in PDO. NULL values were returning as null, but integers and floats weren't, so I decided to go with a simple solution. It's not tested on all database types, so if you think it needs improvement, let me know (my twitter is "francanobr") <?php // Returns the contents of $stm (PDO Statement) // as an associative array, with correct native types // for integers, nulls, and floats $array = []; while ($fila = $stm->fetch(\PDO::FETCH_ASSOC)) { $i = 0; foreach ($fila as $key => $value) { $columnMeta = $stm->getColumnMeta($i); switch ($columnMeta['native_type']) { case 'LONG': case 'TINY': $fila[$key] = intval($value); break; case 'DOUBLE': $fila[$key] = floatval($value); break; } $i++; } $array[] = $fila; } return $array; ?> The code sample is part of a function, the function declaration is not included, evaluate how to integrate it with your app. ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.168 (proxied: 81.184.115.41) ---- Manual Page -- http://php.net/manual/en/pdostatement.getcolumnmeta.php Edit -- https://master.php.net/note/edit/124111 Del: integrated -- https://master.php.net/note/delete/124111/integrated Del: useless -- https://master.php.net/note/delete/124111/useless Del: bad code -- https://master.php.net/note/delete/124111/bad+code Del: spam -- https://master.php.net/note/delete/124111/spam Del: non-english -- https://master.php.net/note/delete/124111/non-english Del: in docs -- https://master.php.net/note/delete/124111/in+docs Del: other reasons-- https://master.php.net/note/delete/124111 Reject -- https://master.php.net/note/reject/124111 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211883) next »