PDA

View Full Version : Extract correct values from Mysql


mapg
07-30-2004, 09:43 AM
I have a problem to extract values from Mysql

Taking this mysql table ...

Table name: access

id ip country first_access
1 80.14.97.53 fr 2004-07-27 08:26:38
2 62.34.171.112 fr 2004-07-27 08:30:50
3 213.5.67.240 gr 2004-07-27 08:30:55
4 10.140.209.115 us 2004-07-28 08:31:42
5 217.126.113.147 es 2004-07-28 08:33:24
6 24.157.36.155 ca 2004-07-28 08:35:12
7 218.186.147.156 sg 2004-07-29 08:36:45
8 219.110.41.1 jp 2004-07-29 08:37:19
9 65.54.164.135 us 2004-07-29 08:37:50
10 67.15.35.8 us 2004-07-30 08:43:38
11 195.34.228.140 ru 2004-07-30 08:44:31
12 217.82.81.59 de 2004-07-30 08:55:16... and with this PHP code ...


$access_avg_per_day = @mysql_query("select first_access from `access` group by date_format(first_access, '%Y-%m-%d') asc");
while ($ctime = @mysql_fetch_array($access_avg_per_day)) {
$time_data[] = $ctime['first_access'];
}

echo print_r($time_data);... I just get an array like that ...

Array ( [0] => 2004-07-27 08:26:38 [1] => 2004-07-28 08:31:42 [2] => 2004-07-29 08:36:45 [3] => 2004-07-30 08:43:38 ) Namely the first timestamp of everyday.

... BUT I would like to extract the set of timestamps per day and not the first timestamp only.

This case could summarized as ... "to extract dates and times stamps per day ... grouped just by date"

Any help is really welcome. Thank you in advance.

Mapg

mapg
07-30-2004, 12:15 PM
A PHP programmer said me the response: A two-dimensional array.

$sql = "select first_access, date_format(first_access, '%Y-%m-%d') as date_day from `access` order by date_day asc";
$access_avg_per_day = mysql_query($sql) or trigger_error(mysql_error());
$time_data = array();
while ($ctime = mysql_fetch_array($access_avg_per_day)) {
$day = $ctime['date_day'];
if(!isset($time_data[$day]))
$time_data[$day] = array();
$time_data[$day][] = $ctime['first_access'];
}
print_r($time_data);And works!!. But what I see is that I am not used to handle a two-dimensional array. :-(

I do not know who start to organize the arrays in $time_data to separate arrays per day.

I have done several tests but with no success.

Sorry if I am so amateur in PHP programming. :-(

Any help is really welcome.

Mapg

BTW: For administrator: This thread should be moved to PHP forum because its solution is not a Mysql issue yet.