Jump to content

Having trouble with Array value saying "null"?


PHP_GreenHorn

Recommended Posts

My PHP Code:

 

//the SQL query to be executed
$query = "SELECT age, SUM(consumer_income) FROM table GROUP BY age";
            
//storing the result of the executed query
$result = $conn->query($query);
          
//initialize the array to store the processed data
$jsonArray = array();
 
//check if there is any data returned by the SQL Query
if ($result) {
 
 //Converting the results into an associative array
 while($row = $result->fetch_assoc()) {
   $jsonArrayItem = array();
   $jsonArrayItem['label'] = $row['age'];
   $jsonArrayItem['value'] = $row['SUM(consumer_income)'];
 
   //append the above created object into the main array.
   array_push($jsonArray, $jsonArrayItem);
 }
}

 

-----------------------------------------------------------------------------------------------

 

My output:

[{"label":"13","value":"0"},{"label":"20","value":"0"},{"label":"30","value":"0"},{"label":"40","value":"0"},{"label":"70","value":"0"},{"label":"90","value":"0"},{"label":"100","value":"0"}]

Link to comment
Share on other sites

My guess would be that there is something wrong with the data,

 

What is the table structure?

Can you show us some sample data? For example, are you storing income values with currency symbols?

 

Don't store ages. It constantly needs updating; store the date of birth instead

Thanks. I got it working now.  My table had ($) currency symbols inserted.

Link to comment
Share on other sites

My guess would be that there is something wrong with the data,

 

What is the table structure?

Can you show us some sample data? For example, are you storing income values with currency symbols?

 

Don't store ages. It constantly needs updating; store the date of birth in

 

At first glance I'd question the format of your summary value.  How about using an 'AS newname' clause on your SUM operation?

 

That and the fact that you have it all in quotes thus making it a string for the index name

Thanks. I got it working now.  I changed my SUM operation to use the 'AS newname'.

.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.