I have tried fetching MySQL query results using mysql_fetch_row() and mysql_result() and numeric values are being returned as strings.
Is
I wrote a function to circuvent this (for PDO):
/**
* Converts columns from strings to types according to
* PDOStatement::columnMeta
*
* @param PDOStatement $st
* @param array $assoc returned by PDOStatement::fetch with PDO::FETCH_ASSOC
* @return copy of $assoc with matching type fields
*/
function convertTypes(PDOStatement $statement, $assoc)
{
for ($i = 0; $columnMeta = $statement->getColumnMeta($i); $i++)
{
$type = $columnMeta['native_type'];
switch($type)
{
case 'DECIMAL':
case 'TINY':
case 'SHORT':
case 'LONG':
case 'LONGLONG':
case 'INT24':
$assoc[$columnMeta['name']] = (int) $assoc[$columnMeta['name']];
break;
case 'DATETIME':
case 'DATE':
case 'TIMESTAMP':
$assoc[$columnMeta['name']] = strtotime($assoc[$columnMeta['name']]);
break;
// default: keep as string
}
}
return $assoc;
}
Of course the type list are not complete and the conversion is oversimplified, but can be useful for start.