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

5
  • Please show your code. Commented Jan 17, 2014 at 15:49
  • What's causing an issue? Commented Jan 17, 2014 at 15:49
  • are you using the same PHP and MySQL version, and above-all, mysqli extension (in case you use that one) version ? Commented Jan 17, 2014 at 15:51
  • 3
    Take a look at this, I think your issue is related to your PHP version difference between the two environments Commented Jan 17, 2014 at 15:51
  • @Asok seems like my issue, but it shows the examples to enable/disable as php code. I have the same code on both live and local so this seems a bit odd to me. Commented Jan 17, 2014 at 16:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.