Welcome Guest, Not a member yet? Register   Sign In
mysql queries using database class return numbers as strings
#1

[eluser]romero619[/eluser]
It appears that the 'result_array()' method of the database class is returning everything as a string, even values that have numeric types within my mysql table.

Is this by design?

From what I can tell, CI uses PHP's native 'fetch_row_assoc()' function to convert the DB query results into an array. Is this where the numbers are being converted to strings, or is it something in the way that CI queries the DB & returns the results that causes the type conversion of all datatypes into strings???

What can be done to correctly return the right datatypes in the query results?

(please dont suggest looping over each&every;variable in the result array & casting it to the right type, since this really isnt feasible for my large result sets).

Please help,
thanks...
#2

[eluser]Dam1an[/eluser]
As far as I know, it doesn't really matter if its returned as a string or an integer, as PHP can switch between the 2 at run time, so the following 2 are effectivly the same;
Code:
$a = '12';
$b = '10';
$c = $a+$b;
echo $c;

$a = 12;
$b = 10;
$c = $a+$b;
echo $c;
#3

[eluser]romero619[/eluser]
the problem is not really in php.
Im using php's 'json_encode' function to output my db results into json.
the json output is showing only string values for everything returned from the db query.

This somewhat creates a problem in later use with javascript (in the project Im working on).

any other advice?
#4

[eluser]Dam1an[/eluser]
Sorry, I just assumed this was regarding PHP
I'm not familiar with JSON/javascript, so not really able to help you there

If only a small subset of the data you're fetching is numeric, instead of converting everything, you could use something along the lines of a try catch, and if it fails (because it expects a number), then you cast it and try again

Or would that also not be feasible on your data set?
#5

[eluser]jedd[/eluser]
We had a [url="http://ellislab.com/forums/viewthread/110393/"]discussion over here[/url] recently on this topic, and there's a few ideas thrown around that might be of use to you.

Oh, and I think you'll find that the problem actually is in PHP.




Theme © iAndrew 2016 - Forum software by © MyBB