Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2004, 02:50 PM
Authorized User
 
Join Date: Sep 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default automatic record delete.

Can I do a automatic record delete? For example, I would have the possibility to select a "option" that delete record in certain date, in automatic mode.

it is possible? (excuse me for my english..eheh:))

thanks

joeore

 
Old April 27th, 2004, 05:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't really know what you mean. Can you be more descriptive? I don't know what you mean by "automatic mode".


Take care,

Nik
http://www.bigaction.org/
 
Old April 27th, 2004, 06:17 PM
Authorized User
 
Join Date: Sep 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello nikolai:), I mean that a record must be deleted when pass a certain time, for example 15 days.

It's possible?

joeore:)

 
Old April 28th, 2004, 09:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can't write anything into the record that effectively gives it a "time to live", but depending on how much access you have to the machine you are running on, you could use something like a cron job (aka. a "Scheduled task" on Windows) or a stored procedure running on the database itself (assuming you database manager supports them - recent versions of MySQL, Postgres and SQL Server do, Access doesn't).

Run it at midnight each day to filter off records you no longer want.
 
Old April 28th, 2004, 08:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Right. Your PHP script would look something like this:

<?php // delete_old.php

// connect to db here...

$expired = date("Y-m-d", strtotime("-15 days"));

$query = "DELETE FROM table WHERE date_col < '$expired'";
mysql_query($query);
?>

On the commandline, you would execute this by:

php -q delete_old.php


strtotime() creates a UNIX timestamp based on a string representation. We create the timestamp for 15 days ago. We use date() to format it into YYYY-MM-DD format, which is how MySQL stores items in DATE columns. (DATE is a MySQL data type).

More info:
  http://www.php.net/strtotime
  http://www.php.net/date


Take care,

Nik
http://www.bigaction.org/
 
Old April 29th, 2004, 11:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or (swiftly dons smart-arse cap, at this point):

date("Y-m-d", strtotime("fortnight 1 day ago"));

A swift guide to the GNU fuzzy-logic relative time functions can be found here.
http://www.gnu.org/software/coreutil...27.html#SEC174

Dan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 06:29 AM
automatic insertion of new record.....help...... abhit_kumar JSP Basics 1 March 4th, 2005 12:50 PM
automatic insertion of new record.....help...... abhit_kumar Pro JSP 0 March 4th, 2005 04:56 AM
Can't delete record Trojan_uk SQL Server 2000 3 November 27th, 2003 01:03 PM
Delete record but how? scifo Beginning PHP 3 August 1st, 2003 12:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.