Jump to content

Having trouble with Array value saying "null"?


Go to solution Solved by Barand,

Recommended Posts

Hi All,

 

My concept:

i'm trying to select age and salary from my database. i want to get the SUM of salaries of each age group. Then i want to convery it to JSON, so i can display it in a chart.

 

Output:

[{"label":"20","value":"null"},{"label":"30","value":"null"}]

 

 

Thanks in advance.

 

 

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"}]

Edited by PHP_GreenHorn

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

Edited by ginerjm
  • Like 1
  • Solution

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

  • Like 1

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.

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'.

.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.