Page 1 of 1

Database is returning strings from integer columns

Posted: 05 June 2020, 22:51
by Brainchild
Hello, starting today I've noticed a weird behaviour that I haven't seen before. I can't say for sure it's new, but the fact that I've noticed it twice today suggests something may have changed in Studio recently.

When I query the database it is returning strings for columns that are int type. I've observed this in my own custom table with getCollectionFromDb(), and in the `global` table used by getGameStateValue(). In both cases I was able to resolve the problem by casting to int in PHP, but it seems like I shouldn't have to.

Has anyone else noticed this?

Here's the table definition in case it helps:

Code: Select all

CREATE TABLE IF NOT EXISTS `gold_zone` (
  `slot_num` INT UNSIGNED NOT NULL,
  `gold` INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`slot_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And the PHP:

Code: Select all

    function getAllGoldOnTable()
    {
        $sql = "SELECT slot_num, gold FROM gold_zone";
        return self::getCollectionFromDb($sql);
    }

            foreach ($this->getAllGoldOnTable() as $slot_num => $gold_zone)
            {
                $gold = (int)$gold_zone['gold']; <-- this is a string

Re: Database is returning strings from integer columns

Posted: 05 June 2020, 23:09
by hersh
That’s the way it works.

Re: Database is returning strings from integer columns

Posted: 06 June 2020, 11:16
by Brainchild
Thanks, apparently I didn't do enough Googling before posting this thread. Indeed, it seems PHP returns MySQL results as strings, regardless of their actual type. I will add a note to the BGA Studio wiki.

https://stackoverflow.com/questions/532 ... ing-in-php
https://stackoverflow.com/questions/501 ... ger-in-php