0

I really don't know if this is a MySQL or PHP issue. On my live server integer columns are returned as strings which is the way I've always had it. However on my local server they are returned as integers instead of strings.

I searched around but other than comments about typecasting there is nothing about some kind of config parameter. I'm using laravel 4 and it' the same code on both local and y live server.

I don't want to typecast everything and just prefer it to be returned as strings. I'm using wamp locally and the versions for mysql and php between my server and local or very close so I imagine it's some kind of config somewhere?

EDIT:

Code is straight forward:

$word = Word::first(array('status'));

echo $word->status === '1' ? 'true' : 'false'; // returns false
echo $word->status === 1 ? 'true' : 'false'; // returns true

For my MySQL and PHP versions they are almost the same:

Live: php5.4.20, mysql5.5.34 Local: php5.4.16, mysql5.6.12

Rob
  • 10,851
  • 21
  • 69
  • 109

3 Answers3

3

I'm still not exactly clear where this discrepancy may be between my environments, but in Laraval at least, you can set the pdo attribute in the database config:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'xxxxxxx',
    'username'  => 'xxxxxxx',
    'password'  => 'xxxxxxx',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'options'   => array(
        PDO::ATTR_STRINGIFY_FETCHES => true
    )
),

I guess this at least helps keep it consistent regardless of what other developers may have setup locally.

Rob
  • 10,851
  • 21
  • 69
  • 109
1

Also, there is type-checking when using the triple equal operator ===. Before any value comparisons are done, the type of the two entities are compared. If the two types don't match, false is returned immediately.

Using == will return consistent results in this case, but === is often used in JavaScript to maintain datatype integrity.

Joe
  • 797
  • 1
  • 10
  • 23
0

In Laravel 4, you can set Accessors & Mutators to normalize the values returned from your database. Example:

//Word.php

class Word extends Eloquent
{

    //...

    public function getStatusAttribute($value)
    {
        return strval($value);
    }
}
kevincolten
  • 139
  • 1
  • 9