Welcome Guest, Not a member yet? Register   Sign In
count_all_results returns a string?
#1

[eluser]fritzthecat[/eluser]
Hi. It appears that count_all_results returns a string.
Surely it should be an integer?

thanks
#2

[eluser]TheFuzzy0ne[/eluser]
I think that's how the database returns it.
#3

[eluser]fritzthecat[/eluser]
(I was talking about mySQL btw)

yes but isn't the idea of frameworks to smooth out odd behaviours like that?
#4

[eluser]jedd[/eluser]
Yes and no.

There isn't a 1:1 mapping of datatypes between MySQL and PHP, so there will always be exceptions and complications to deal with here.

Having said that, there is a reasonable expectation that numeric results return as numeric datatypes rather than string. OTOH there's a certain reliability about knowing that PHP will take every MySQL result and present it as a string type.

There was a [url="http://ellislab.com/forums/viewthread/110393/"]thread recently on this subject[/url] and Samuel came up with a nifty helper function that will meet you half way with this. Alternatively you can hack the core DB code if you want to get extra points.

Note that this is a PHP problem / feature - you can test it with the half dozen MySQL libraries for PHP (though there's really only one in use these days) to confirm for yourself. It'd be a big call for a framework to start munging datatypes on the way through - it would make your code cleaner, yes, but it would also make your code totally reliant on that framework from that point on. This is probably not a good thing for a framework to do.
#5

[eluser]daveWid[/eluser]
For a quick change to an integer you can just cast it. There are a few different ways.

Code:
$results = (integer)$this->db->count_all_results('my_table');
$results = (int)$this->db->count_all_results('my_table');
$results = intval($this->db->count_all_results('my_table'));
#6

[eluser]fritzthecat[/eluser]
Hi, thanks for the comprehensive reply.

Yes I accept that mySql returns strings in a few places where you don't expect it to, but I still think CI should make its own decision on the matter. Otherwise if you change db driver to, say, MS-SQL and that returns and integer, your code may break. I'd prefer if CI was consistent across drivers, and if some behave oddly then correct them.

As a thought experiment, imagine the scenario where you cache a query and then use a custom extension similar to count_all_results on the cached version - presumably you'd do that by counting the cached rows in an array. Would you then cast that count to a string, to replicate mySql's illogical behaviuour? It'd be strange.

As for coders' expectations, when coding directly in mySql I am fully aware count returns and a string, but when using count_all_results I was in 'CI' mode, and my first instinct was to expect it to return an integer. They are two different contexts, I don't think there'd be any confusion if they behaved differently.

Anyway, that's just my two cents, and it's not that important after all.
#7

[eluser]TheFuzzy0ne[/eluser]
Perhaps CodeIgniter should, but it all comes at the cost of speed. I've never had a problem making comparisons or doing math, and if I did, I'd cast it.

Code:
$str = "1";
$num = 1;

echo "\n";

echo '($num == $str): ';
echo (($num == $str) ? 'Yes' : 'No');
echo "\n";

echo '(($num + $str) == 2): ';
echo ((($num + $str) == 2) ? 'Yes' : 'No') . "\n";
echo "\n";

PHP is a loosely typed language, and that's what makes it so great. I think that helps you write less code. After all, PHP scripts are parsed and compiled at run time, they are not precompiled like Java or C#/C++ applications. Also, PHP is meant to be simple, and easy to learn and use. IMHO this accounts for a lot of PHP's popularity over other server-side scripting languages. The other major part is that it's free.
#8

[eluser]fritzthecat[/eluser]
@TheFuzzyOne: I'm glad you find PHP so useful and never had any problems with types. Also I'm sure someone will find yours and @daveWid casting examples useful. However I DID have code breaking because it was expecting an integer and count_all_results returns a string. And yes, I did solve the problem by casting, but that's not the question.

The question I am asking - does it make sense to have a function called 'count_something' return a string when a count is blatantly an integer?
#9

[eluser]Thorpe Obazee[/eluser]
@fritzthecat. It is a common security measure to have most data that may or may not break your code evaluated.
#10

[eluser]fritzthecat[/eluser]
I wonder about the people who write comments in this forums.

Apart from jedd, who read the question and wrote a well-argued rebuttal, everyone else just seems to be either unhelpful or patronizing or, as in bargainph's case, a sarky smartass.




Theme © iAndrew 2016 - Forum software by © MyBB