I'm pulling numerical data from the database based on a date selected by the user. For some reason, when I select my function to pull all of today's data (there is none), it still forms an array of null values.
I'm thinking the problem lies in either the select_sum() or where() functions. Here's my CI query:
$this->db
->select_sum('column1')
->select_sum('column2')
->select_sum('column3')
->where('created_at >=', date('Y-m-d'));
$query = $this->db->get('table_name');
And here is my foreach loop that pulls all of the selected data into an array to be used throughout the page:
$popular_items_array = array();
foreach ($query->result() as $key => $row)
{
if ($row == NULL) {
echo "Error populating table.";
} else {
$popular_items_array = $row;
}
}
To take a look at the data, I then did:
echo json_encode($popular_items_array);
Which turns out showing this:
{"column1":null,"column2":null,"column3":null}
If I select a different time frame (where data actually exists by the set date) that same JSON echo will display the existing data. The thing I'm not understanding is, why is the query returning anything at all? Why isn't it just failing? And how can I run a check / loop that will catch this problem, and display an error message letting the user know that no data exists on that date?
column1
) AS column1, SUM(column2
) AS column2, SUM(column3
) AS column3 FROM (table_name
) WHEREcreated_at
>= '2013-07-01' – PrinceEnder Jul 1 '13 at 18:40