The histogram counting code needs to run through the whole data array, not just the first 50 elements.
You should also be checking that 'a' is within the bounds 0 <= a < 50, otherwise, if you get a value from the db that's greater then 49 (or less that 0), your histogram[a]++ statement will overrun the array bounds and cause "bad things" to happen.
There's also no need to do the two operations separately - you can read the data and collate the histogram in the first for-loop; this saves you a lot of time running through the array twice.