Auto Calculate Age on all Records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Auto Calculate Age on all Records

Post 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!
Last edited by Benjamin on Fri Oct 29, 2010 4:49 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Auto Calculate Age on all Records

Post 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
Last edited by Benjamin on Fri Oct 29, 2010 6:19 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
Post Reply