Reputation: 69
I'm quite new in PHP dev and I can't find a way to solve my problem.
I have to clean a some data in my Mysql database. I have a date
column in my DB full of time periods like
-520;-510;0;-500;-530;-599;-499;-510;-490;-500;0;-450
and I want to extract the min and max value from it, so that I will only have 2 time period left.
I tried this :
$sql = "SELECT date FROM records WHERE serial = 1";
$requete = mysql_query($sql,$cnx) or die(".mysql_error().");
while($result = mysql_fetch_array($requete))
{
$res = $result[date];
$min = min($res);
$max = max($res);
}
I understand that it can't work because min and max functions are only working with an array but I don't know how to tranform my $result[date]
in an new array.
Upvotes: 1
Views: 7772
Reputation: 477
assuming that the DB "date" field contains a string of numbers seperated by ; like you described then you can use the explode() function to turn that string into an array and then use max() and min() on that array
$arr = explode(";",$string);
$max = max($arr);
$min = min($arr);
if it s a one number per row then do it with SQL like described in the other answer
Upvotes: 0
Reputation: 59709
Do this in SQL, something like this should work:
$sql = "SELECT MAX( date) as maximum, MIN( date) as minimum
FROM records
WHERE serial = 1
GROUP BY serial";
Then to retrieve the result, you need one row:
$requete = mysql_query($sql,$cnx) or die( mysql_error());
$row = mysql_fetch_array($requete);
echo $row['maximum'] . ' ' . $row['minimum'];
Upvotes: 5