Reputation: 1
I'm new here. So, be easy on me. I have a table called 'data' in which one of the columns is 'date' stored in YYYY-MM-DD format. Here is the code that I have been working on. Basically, what I want to do is compare if two dates stored in my table are equal. But, each time I run the code, I keep getting error: Undefined offset right where the code says $lisdate[$i+1]. How do I compare the dates stored in a table? Thank you.
My code
$sth2 = mysql_query("SELECT * FROM data WHERE dest_name='www.myren.net.my'");
while($rowstemp = mysql_fetch_assoc($sth2))
{
$lisdate[] = $rowstemp['date'];
$lisaverage[] = $rowstemp['avg_rtt'];
}
$rows = mysql_num_rows($sth2);
$addAverage[0] = $lisaverage[0];
$numbers = 0;
$j = 0;
for($i = 0; $i<$rows; $i++)
{
if($lisdate[$i] == $lisdate[$i+1])
{
$addAverage[$j] = $addAverage[$j] + $lisaverage[$i+1];
}
else
{
$j++;
$numbers = $numbers +1;
}
}
Upvotes: 0
Views: 184
Reputation: 108450
It's not entirely clear what that code in the question is attempting to achieve.
(As Ollie pointed out in his answer, it bears repeating: the order that MySQL returns the rows is guaranteed ONLY if there's an ORDER BY
clause on the query. The result from a "duplicate date" check that's being performed in the code is only going to detect a "duplicate" date value if it appears on contiguous rows.)
We see the code loading a couple of arrays, using the date
and avg_rtt
column values returned by the query. Then there's some manipulation on the array... the end result will be another it's not clear what the actual intent of that rigmarole is, what it's actually trying to achieve.
If there are any rows It looks like there's going to be another array... if there are no "duplicate" date values found, it's going to have a single element (with the value of rtt_avg
from the first row), and $numbers
will be set to the number of elements in the original array.
If there is a "duplicate" date values found, the results from the code seem very odd to me, a sparsely populated array. Why?
Personally, I'd be looking to get an actual statement of the specification, and have the database do that processing for me, rather than mucking through two arrays.
If what we want is set of values with no duplicated date values, I'd use a GROUP BY
and some aggregation, for example:
SELECT d.dest_name
, d.date
, AVG(d.avg_rtt) AS avg_avg_rtt
, MAX(d.avg_rtt) AS max_avg_rtt
, MIN(d.avg_rtt) AS min_avg_rtt
, SUM(d.avg_rtt) AS sum_avg_rtt
, COUNT(1) AS cnt
, COUNT(d.avg_rtt) AS cnt_avg_rtt
, COUNT(DISTINCT d.avg_rtt) AS cnt_distinct_avg_rtt
FROM data d
WHERE d.dest_name = 'www.myren.net.my'
GROUP
BY d.dest_name
, d.date
ORDER
BY d.dest_name
, d.date
If I was looking for just a count of distinct date
values, like what $numbers
is going to contain, then just:
SELECT d.dest_name
, COUNT(DISTINCT d.date) AS cnt_distinct_date
FROM data d
WHERE d.dest_name = 'www.myren.net.my'
GROUP BY d.dest_name
ORDER BY d.dest_name
The "looping through array" that the original query just looks like an odd way to achieve a result, whatever that result is supposed to be.
Upvotes: 0
Reputation: 108746
Gordon has it right. Your for loop is overrunning the end of your array because you're working with consecutive pairs of rows. Change it to read ; $i < $rows - 1;
to correct this problem.
But, you have another problem. SQL rows in result sets have an unpredictable order unless your query includes an ORDER BY clause. If these rows, without that clause, appear in ascending order by date
, it's dumb luck. Put ORDER BY date
in your query.
Upvotes: 2