1

I am using codeigniter, and when selecting from the MySQL DB, an integer, I get a string, like "1", and I want it to return 1.

Here is a solution to this problem, using PDO: How to get numeric types from MySQL using PDO?

I don't know how to apply it to my situation, using Codeigniter 3, and PHP 7 (it uses the mysqli driver)

PHP type casting is too slow for me, Here is a post about it (so please don't suggest doing this): PHP - Recursive in place replacing of strings to numbers takes too long

Community
  • 1
  • 1
Amit
  • 5,924
  • 7
  • 46
  • 94
  • 3
    How are you using this integer? And why is typecasting too slow? – Qirel Jun 25 '16 at 08:42
  • @Qirel I am responding this data, using json_enocde. Why is it slow? On 200MB, it takes so so long, when the query and download takes about 5 seconds. (so long, it doesn't stop after 30 seconds) – Amit Jun 25 '16 at 08:44
  • `intval()` http://php.net/manual/en/function.intval.php, type casting is to slow? pentium 2 CPU? I have production sever that runs 50k queries per second, I doubt casting will impact performance much. – ArtisticPhoenix Jun 25 '16 at 08:56
  • @ArtisiticPhoenix Here is a test on small data compared to what I have: http://sandbox.onlinephpfunctions.com/code/5b261993efbc969cfd7c4df9f03a1b5bb0d6b460 , BTW, intval is slower than (int), slower than 0+"1" – Amit Jun 25 '16 at 09:00
  • @Amit - so you plan to do 320k iterations with this function. Even then it's 2 tenths of a second, that well below the time the network traffic takes. – ArtisticPhoenix Jun 25 '16 at 09:07
  • see [MYSQLI_OPT_INT_AND_FLOAT_NATIVE](http://docs.php.net/manual/en/mysqli.options.php). – VolkerK Jun 25 '16 at 09:13

1 Answers1

1

It depends on the mysql client driver.

  • The original mysql client driver returned all data as strings.
  • The "new" mysqlnd (native driver) returns strongly typed data where possible.

This driver has been the default since PHP 5.4, so you should already be using it.

You can use phpinfo() to find out which driver you are using for sure. Look for the MySQL section and it will say something like:

Client API version  mysqlnd 5.0.11-dev 
scipilot
  • 6,681
  • 1
  • 46
  • 65
  • I just installed latest xampp version. Where can I find download for the driver? – Amit Jun 25 '16 at 09:32
  • Well according the mysqlnd page, PHP 5.4 has mysqlnd as default so you should be using it already. See updated answer for how to tell. Perhaps CodeIgniter is doing something in between to convert back to text. – scipilot Jun 26 '16 at 01:06