RedCitrus
RedCitrus

Reputation: 1

Unwanted truncation of PHP array key name

Bit of a MySQL newbie, so apologies if this is a simple issue or terminology incorrect. I'm trying to create a dynamically-generated MySQL queries which interrogates data stored in the database. Up until now this has worked perfectly, however, for my most recent query the array key name seems to be truncated. If I run the MySQL query directly in PhpMyAdmin, it works fine, but when I do it within my site the output is truncated. For example, my query is as follows:

SELECT smallsites_borough_id,
smallsites_borough,
(((smallsites_propertytype_1bed_2015 * 2) + (smallsites_propertytype_2bed_2015 * 3) + (smallsites_propertytype_3bed_2015 * 5) + (smallsites_propertytype_4bed_2015 * 6)) / (smallsites_area_hectares * (1 - (smallsites_percent_greenbelt + smallsites_percent_mol))))
FROM smallsites_data
ORDER BY (((smallsites_propertytype_1bed_2015 * 2) + (smallsites_propertytype_2bed_2015 * 3) + (smallsites_propertytype_3bed_2015 * 5) + (smallsites_propertytype_4bed_2015 * 6)) / (smallsites_area_hectares * (1 - (smallsites_percent_greenbelt + smallsites_percent_mol))))
DESC

When I run print_r on the resulting output, I get the following (first line only):

Array (
[smallsites_borough_id] => 29
[smallsites_borough] => Islington, London Borough of
[(((smallsites_propertytype_1bed_2015 * 2) + (smallsites_propertytype_2bed_2015 * 3) + (smallsites_propertytype_3bed_2015 * 5) + (smallsites_propertytype_4bed_2015 * 6)) / (smallsites_area_hectares * (1 - (smallsites_percent_greenbelt + smallsites_percent_] => 231.33950480916354
)

You'll see above that the last part of the key is being truncated (at smallsites_percent_). Why? Is this a limitation of print_r, or is it actually the case that it can't handle an array with a key name of this length? I'm trying to return the end figure, ie. the value 231.33950480916354, however, when I run the following:

while ($array = $result->fetch_assoc()) {
echo $array[$sql_query];
}

...it returns zero. I know that the underlying code is sound, because I have over 90 other queries set up in exactly the same way which work perfectly - but with shorter queries. So it can only be that somehow the array key is being truncated, as print_r would suggest. I understood that there was no limit to function or variable names in PHP, so I don't understand why this is happening. Please help!

Upvotes: 0

Views: 133

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94672

You can give any column an Alias (another name to be used in the resultset), but it is always a good idea to do this on a calulation column like that

SELECT smallsites_borough_id,
        smallsites_borough,

        (((smallsites_propertytype_1bed_2015 * 2) +
             (smallsites_propertytype_2bed_2015 * 3) + 
                (smallsites_propertytype_3bed_2015 * 5) + 
                (smallsites_propertytype_4bed_2015 * 6)) / 
            (smallsites_area_hectares * (1 - (smallsites_percent_greenbelt + 
            smallsites_percent_mol)))) AS SensibleName  
/*    change here                      ^^^^^^^^^^^^^^^  */

FROM smallsites_data
ORDER BY (((smallsites_propertytype_1bed_2015 * 2) + (smallsites_propertytype_2bed_2015 * 3) + (smallsites_propertytype_3bed_2015 * 5) + (smallsites_propertytype_4bed_2015 * 6)) / (smallsites_area_hectares * (1 - (smallsites_percent_greenbelt + smallsites_percent_mol)))) DESC

Upvotes: 1

Related Questions