Page 1 of 1

Auto Calculate Age on all Records

Posted: Fri Oct 29, 2010 4:02 am
by Stela
Hello...

I need to update once per month all the ages of a DB with 1000 records.

The DB as the Birthday and I did this code (incomplete)

Code: Select all

include("ConnectDBParadiso.php");
mysql_select_db("$ConnectDBParadiso") or die ("Erro ao ligar à Base de Dados da Sonika-Event");

$select = "SELECT * FROM tabClientes";
$result = mysql_query($select);
$size = mysql_num_rows($result);
echo $size;

$i = 0;
while($result_ar = mysql_fetch_assoc($result)){
	$varIdade = "28";
	$update = "UPDATE tabClientes SET Idade = '$varIdade' WHERE $i = $i";
	mysql_query($update);
  	$i+=1;
}

date_default_timezone_set('Europe/London');
function functCalculateAge($birthday){  
    return floor((time() - strtotime($birthday))/31556926);
}

I'm with this a few days and can't get it to work. Tried many things but with no success.

Thanks!

Re: Auto Calculate Age on all Records

Posted: Fri Oct 29, 2010 5:54 am
by klevis miho
Change the while loop to this:

Code: Select all

while($result_ar = mysql_fetch_assoc($result)){
        $varIdade = "28";
        $update = "UPDATE tabClientes SET Idade = '$varIdade' WHERE id = $result_ar[id]";
        mysql_query($update);
        $i+=1;
}

Where I'm suposing that id is the id of the the row in tabClientes table