Page 1 of 1

Strange mysql result

Posted: 22 March 2017, 10:11
by EnergyBear
Hi !

I have a table with that structure :

Code: Select all

CREATE TABLE IF NOT EXISTS `tfs_gameboard_tiles` (
  `position_h` int(1) NOT NULL,
  `position_v` int(1) NOT NULL,
  `card_id` int(10) DEFAULT NULL,
  `properties` text,
  `player_id` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
It is harvested with that data:

Code: Select all

INSERT INTO `tfs_gameboard_tiles` (`position_h`, `position_v`, `card_id`, `properties`, `player_id`) VALUES
(0, 1, NULL, NULL, NULL),
(0, 2, NULL, NULL, NULL),
(0, 3, NULL, NULL, NULL),
(1, 0, NULL, NULL, NULL),
(1, 1, NULL, NULL, NULL),
(1, 2, NULL, NULL, NULL),
(1, 3, NULL, NULL, NULL),
(1, 4, NULL, NULL, NULL),
(2, 0, NULL, NULL, NULL),
(2, 1, NULL, NULL, NULL),
(2, 2, NULL, NULL, NULL),
(2, 3, NULL, NULL, NULL),
(2, 4, NULL, NULL, NULL),
(3, 0, NULL, NULL, NULL),
(3, 1, NULL, NULL, NULL),
(3, 2, NULL, NULL, NULL),
(3, 3, NULL, NULL, NULL),
(3, 4, NULL, NULL, NULL),
(4, 1, NULL, NULL, NULL),
(4, 2, NULL, NULL, NULL),
(4, 3, NULL, NULL, NULL);
But, with the SQL query

Code: Select all

self::getCollectionFromDb("SELECT * FROM tfs_gameboard_tiles")
, I have this result:

Code: Select all

array(5) {
  [0]=>
  array(5) {
    ["position_h"]=>
    string(1) "0"
    ["position_v"]=>
    string(1) "3"
    ["card_id"]=>
    NULL
    ["properties"]=>
    NULL
    ["player_id"]=>
    NULL
  }
  [1]=>
  array(5) {
    ["position_h"]=>
    string(1) "1"
    ["position_v"]=>
    string(1) "4"
    ["card_id"]=>
    NULL
    ["properties"]=>
    NULL
    ["player_id"]=>
    NULL
  }
  [2]=>
  array(5) {
    ["position_h"]=>
    string(1) "2"
    ["position_v"]=>
    string(1) "4"
    ["card_id"]=>
    NULL
    ["properties"]=>
    NULL
    ["player_id"]=>
    NULL
  }
  [3]=>
  array(5) {
    ["position_h"]=>
    string(1) "3"
    ["position_v"]=>
    string(1) "4"
    ["card_id"]=>
    NULL
    ["properties"]=>
    NULL
    ["player_id"]=>
    NULL
  }
  [4]=>
  array(5) {
    ["position_h"]=>
    string(1) "4"
    ["position_v"]=>
    string(1) "3"
    ["card_id"]=>
    NULL
    ["properties"]=>
    NULL
    ["player_id"]=>
    NULL
  }
}
Do you know if the BGA framework is rewriting my query (with a limit somewhere) ?

Thanks guys!

Re: Strange mysql result

Posted: 22 March 2017, 15:43
by docthib
Hi,

It looks like the query took one of each 'position_h' value as key and last entry with same 'position_h' as value.

Seems like a 'normal' behaviour for self::getCollectionFromDB( $sql, $bSingleValue=false )
See doc

For the result you want, I guess you need to add a unique ID to your table or use the self::DbQuery() function

Re: Strange mysql result

Posted: 22 March 2017, 21:37
by Morgalad
It is easier than you think, you need to add a key to the table

use this to the create table stament

Code: Select all

CREATE TABLE IF NOT EXISTS `tfs_gameboard_tiles` (
  `row_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `position_h` int(1) NOT NULL,
  `position_v` int(1) NOT NULL,
  `card_id` int(10) DEFAULT NULL,
  `properties` text,
  `player_id` int(10) DEFAULT NULL,
   PRIMARY KEY (`row_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;