Database is returning strings from integer columns

Game development with Board Game Arena Studio
Post Reply
User avatar
Brainchild
Posts: 71
Joined: 31 January 2014, 19:42

Database is returning strings from integer columns

Post 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
User avatar
hersh
Posts: 76
Joined: 12 December 2013, 23:49

Re: Database is returning strings from integer columns

Post by hersh »

That’s the way it works.
User avatar
Brainchild
Posts: 71
Joined: 31 January 2014, 19:42

Re: Database is returning strings from integer columns

Post 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
Post Reply

Return to “Developers”