Nov 21, 2009

Auto Pruning An Sql Database With Php - How can i do this?

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

Auto Pruning An Sql Database With Php - How can i do this?

shadowx
Hey all.

Now i have a DB, an SQL DB, and i need to auto prune the data there to delete rows lder than a certain time, lets say 2 months for now, the question is how do i do this?

The obvious thing is just to add a field which is the numerical representation of the month when the data is entered and every time the DB is accessed it will check this number against the current month,in a numerical format, and if the difference is two or greater that row is deleted, so if the month is January, it would be O1 and if the current month is march then it would be O3 and the difference is 2 so it would be deleted. This sounds fine untill you take into account how months work... it goes from 1-12 then back to one again! i guess i can overcome that but it seems complicated.

Now im thinking perhaps SQL has a built in way of comparing dates? if so what is it?!

I could simply delete any entries after a certain number of rows but if this website becomes popular then this number of rows might be exceeded in a few days or less so even recent entries would be deleted.

Any ideas?

Thanks

 

 

 


Comment/Reply (w/o sign-up)

ghostrider
Use your idea of adding a field with the month, but just keep incrementing the months as the years go by. For example, January would be one (or whatever month you choose to start on), Februrary would be two, but January of next year would 13. Its not as hard as it sounds. You can find out what month it is based on this equation.

Let y = year you started
Let m = current month
Let z = current year

12(z - y) + m

Does this help?

Comment/Reply (w/o sign-up)

shadowx
Ah yes, that makes sense, so long as i use the year as a four digit number, not a 2 digit because every change of the decade would screw it up!

thanks for that, i shall try that when i get in from college later, it would be interesting to know how Microsoft Access, the DB application, does this because yu can compare dates directly in that and as far as i know it is just a front end application for an SQL database, though i might be wrong.

Comment/Reply (w/o sign-up)

slushpuppy
Apologies, I didn't realise that the question has already been answered.

Comment/Reply (w/o sign-up)

shadowx
Thats ok if you have a different solution then id be interested to hear it, at the moment ive got ghostrider's solution in the works but im still interested at different angles to approaching the problem for future reference, and it might help someone else out too smile.gif


By the way Ghostrider, thanks, it seems to be working which is good!

Comment/Reply (w/o sign-up)

(G)jcbones
Simple Solutions
Auto Pruning An Sql Database With Php

 mysql has date functions that work great for this.

set a column for the current date and call it with now() or update on change.


Next you can delete the row with.


DELETE FROM `table` WHERE `date` < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 2 MONTH)

 

-reply by jcbones


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : auto, pruning, sql, database, php,

  1. Ms-access Database Question
    Allowing Web Access to the Informaton (3)
  2. How Do I Connect To Live Database With Php Script?
    while being hosted with ComputingHost (6)
    I am not new to programming. I want to create a form to add some values into my tables, the code
    are all working. But I am not sure what is the URL to connect to my site's database. All along,
    I have been testing through MAMP, which provides a local copy of mySQL. Can anyone lend me a hand?....
  3. Delete Problem With Id
    Id as auto_increment, and identifier for a row (4)
    Whenever I use 'delete from table where id = 4' from a table with several rows up to 10,
    the 4th row is removed and the update simply shows the rest from 1 to 10 without 4. This is not what
    I want. I want the table to now read 1 through 4 to 9 so that futher delete actions will be in that
    sequence. I want to identify my row with the id (number). Can anybody help out here. Very urgent.
    Serialmike.....
  4. Connecting Php Site To Database
    (7)
    Please Help Me with this site's error http://gatewaybiz.x10hosting.com/surf/ ....
  5. Can Database Column Names Start With A Number?
    Can database column names start with a number? (1)
    Can database column names start with a number or must they start with a letter like php vars? Like
    my_table.1fieldname ? Does this vary from db system to db system? Is it considered good/bad form? ....
  6. Read Xml And Import Into Database
    question (2)
    hi all , i have one big problem i need to know , how i can read xml file and then save strings in
    xml in database for example its my xml CODE 1 2 i want my soruce read this xml
    file and then save string into arash field in mysql database for example in this source arash
    string is " 1" and kiarash string is "2" my fields in mysql are standard and we have 2 field with
    names "arash" and "kiarash" please help me , if you know some tut about this subject thanks
    /wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> ....
  7. Download Database Backup
    Download Database Backup (0)
    Hi all i want write backup system like Phpmyadmin export in single file , my question is what is
    backup query ? i must write manul query for backup for example loop query to get string form
    database and write into one file or php have backup query ? thanks /wink.gif"
    style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> ....
  8. Editing Information In A Mysql Database And Deleting Rows
    (5)
    I need help with a couple of things. First, I need to know how to retrieve information from a mysql
    database and edit it then re add it to the database. I also would like to know how to easily delete
    a mysql row. And I want this done without going to phpMyAdmin. Thanks for the help!....
  9. Importing .csv Into Mysql Database
    NEED HELP! (6)
    I need help importing a .csv contents into a Mysql database. I have this. But its not working.
    CODE connection = mysql_connect("******", "**********", "********") or die ("Unable to connect
    to server"); $db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable to select
    database"); fopen ('csvranks.csv', 'r'); mysql_query("INSERT INTO test_table (id,
    name, guild, level, exp) ?> What am I doing wrong? The mysql table is all set up.. Any help
    is GREATLY appreciated. If you need more info just ask. Thanks! EDIT: The CSV file looks l....
  10. Php Search Engine Script For Mysql Database
    (11)
    A search engine is provided to facilitate the user with undemanding and clear-cut search options.
    The search facility includes simple search, search by title, search by word/phrase, ect… Thus, the
    user is at a safe distance from the risk of selecting files/folders ambiguously. In addition, a
    history of recent searches can be preserved for future perusal. Now
    day’s visitors have a large option for his needs on internet and so visitors are not vesting their
    time by following the dead links on your site. So a search engine is essential for your....
  11. Problem To Join Tables In Database
    Problem in joining (many to many) relation (3)
    Please help me. I'm building a group of database fro a program. the situation is like this:
    'user' may have 1 or more class(es). 'class' itself may have 1 or more user in its
    classroom. I'm bad at explaining... maybe like this: A program is made to write data of
    classroom. Hikaru has a math class at Monday and statistic class at Tuesday. The math class itself
    consists of about 50 students in a classroom. So, Hikaru (user ) may have one or more class at
    time, math class (class) may have one or more students at time. So, basically I must mak....
  12. File In Database
    Question.thanks (1)
    Hi all i write this code i want when you see download.php (this code) my file goes for download for
    user sorry my english is not very good its a file database project for my university but when i
    browse this code my soruce file (.zip) echo in my page and dont goes for download my next querstion
    about safe mode i saw (php.net and zend) when safemode is on file databaseing and header() is not
    working . is it true ? if is it true i save my file at hosting and no (database) i want write
    standard script . plz help me thanks more and more CODE include("config.php"); $kind....
  13. Code To Install Mysql Database Table?
    (5)
    Can anyone show me example code of how to install a database table instead of me having to upload an
    file with it in, so i can do it from a php page? Thanks in advance ;-)....
  14. Showing Numbers Of Mysql Entries In A Database Table
    It should be easy, but I don't know how... (7)
    I have a database, and need to know how to show the numbers of entries. So, if there are 10 entries
    in the selected database table, it will show the number 10 . I'm pretty sure this should be
    easy, but I don't know how to do it. Can someone tell me? /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />....
  15. Requesting Auto Generating Id Tag In Php Code
    Php Coding (3)
    Hello...I'm designing a website in PHP where ppl can submit their links for "cool sites".
    Anyway, when somebody submit's a link to a website for example "http://www.google.com" it
    creates an id such as "index.php?id=1134411593". I dont want the links to be converted into
    id's. I want it to remain as "http://www.google.com". I have the following coding on
    ( echo " ). I'm a novice. Please Help!!!! Thanks... Plus I
    also want to add the date on when the link was submitted. Please follow our forum rule by making....
  16. Crontab Automated Database Backup
    using crontab to make automated database (2)
    i'd like to make an automated database backup as following: 1- i would like to optimize my
    databse's tables. 2- I would like to remove any existing backup tarfile. 3- I dump the database
    to a /tmp file. 4- I tar the database dump and then delete it. i wrote the following shell: CODE
    #!/bin/sh rm -rf /tmp/mybackup.sql.tgz mysqldump -uroot -pmypass --opt mybackup >
    /tmp/mybackup.sql.backup cd /tmp/ tar -zcvf mybackup.sql.tgz mybackup.sql.backup rm -rf
    /tmp/mybackup.sql.backup ....
  17. Suppressing Mysql Error In Php
    How to suppress auto generated mysql err (4)
    I am testing my website on my local machine. It is still in development stage and I am using
    php/mysql. If there is a mysql connection error, I am checking for the connection variable and
    displaying a proper error message. But even before it displays my error message, it displays an
    error message on browser thrown by mysql (MySQL Connection Failed: Access denied for user). Can I
    suppress this message and display only my message?....
  18. Login / Authetication System Using Database
    adding information (4)
    Is there any way to make such database where I can write like name and passwords.. Then make an
    login box, and when somebody trys to acces the login he needs to write the name and password.. Then
    it is verifyed if is there such name and password and if it is then acces the page.. I think there
    is posible something like that with MySQL (db).. but can anybody say me a script or way to make
    something like that? Alredy thanks......
  19. Can You Add Images Into A Mysql Database?
    Using Php? (23)
    I'm learning php in class right now, but I'm still not that good at it, what I'm
    wondering is when I write the php so that it can connect with a database, can I at the same time
    have it that it is able to display back images that I choose. Like, I want a search feature, where
    you can search for a keyword, and it will bring back a list of all the possible entries with that
    keyword, but each of these entries will have a photo associated with it. Now, do I put these image
    files directly into the database, or do I write the code to link them from my files to th....
  20. Is Doing The Whole Database / Php Thing Worth It?
    Question from the inexperianced... (13)
    I'm planning on a site redesign, and I'm going to add some very basic php includes in there,
    because I've managed to figure out how to do them. I wanted to use them to do includes of my
    navigation, so I'd only have to update in one place when new links are added, as opposed to
    having to update several pages all at once. I'm not sure what else php can be used for, and I
    did a search on it, and it pretty much all said includes and databases. Well, databases, I
    don't know how to make, and I was wondering if it's really worth my time to figure i....
  21. Database Extraction Layer
    Does php has Database extraction layer? (1)
    Hi PHP geeks, Do you know if php has Database extraction layer? like Java has. Which is used for
    development of database layer, so it become easy to change the database if you wish to without
    modifying so much code underneath.... Any ideas is welcome. I am sure there should be something
    which is being used and reliable and mature. I am new to PHP so want to use it if it is there. Good
    to know from experienced developer about there developing experience. Thanks in advance.....
  22. Can Reset The Id Auto Increment?
    phpMyadmin (15)
    hi, Can we reset the id auto increment in the mysql database using by phpMyadmin? let's say the
    shoutbox . the id #number table.. somehow i deleted some rows.. and i wanted to re-order/reset the
    id number acendingly in order rather than skip out.. thks ....
  23. Script That Deletes A Row In A Mysql Database Usin
    (1)
    I have just made a script that deletes a row in a mysql database using an image as a button.
    However it will only work in FireFox and not in IE. Can any one see away around this and help me
    root out the problem. This is the script in short: PHP Code: CODE $query_products = "SELECT *
    FROM manufacturer ORDER BY name ASC"; include('external_files/pagination.php');    // If
    the form was submitted, delete the items selected from the database if(isset($_POST )){  $query =
    'DELETE FROM manufacturer WHERE manufacturer_id = '.(int)$_POST ; // or die() d....
  24. Personal Blog
    [ Database Required ] (4)
    Okay, after a week, I have successfully created a personal web blog. I first created it using a flat
    text file, I but realized that it was just too limited like that. So, I redid it in a
    database-compatible manner. NOTES: CODE - This blog does not support comments yet. If you
    want, you can build  on this script by creating a comment system as well. I may also    update it
    with a comment system in the future. - You should have an average to intermediate knowledge of
    MySQL    commands and database manipulation in general, especially if you    want to add a ....
  25. Help Importing Mysql Database Results
    Need help formatting link (3)
    I am having trouble formatting my table to display the results of a mySQL query the way I want.
    Here is the part of my code: CODE $result = mysql_query("SELECT * FROM uploads WHERE
    Category='Drama'") or die(mysql_error()); ?> Title Author File // keeps getting
    the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // echo "
    "; echo $row ; echo " "; echo $row ; echo " "; echo $row ; echo " "; } echo " "; I want to
    make 'Title' show as a hyperlink to 'File' but am at a loss as to how to d....
  26. Writing To Database Problem With Mysql - Not Writing Forum Post
    (3)
    I'm making a forum and I've had several problems tonight which I've come here to ask
    then found the answer to myself, but this one is stumping me. Whenever it goes to write the post to
    the database, it saves the poster, and the time, but the part where the message would go is empty.
    Here's the code, with comments about what it's supposed to do (what I wanted it to do and
    thought it did): CODE $imsg = stripslashes($_POST ); //Get message if(strlen($imsg) > 5 &&
    strlen($imsg) {  $imsg = htmlspecialchars($imsg); //Change stuff to entities  $imsg....
  27. Mysql Edit
    editing info in a MYSQL database (4)
    I have a simple database with a php form thats inserts the info into the database, and another file
    that extracts the info to display the info on another page.... Now my question is ......how do i
    add the EDIT button to edit the database??? here is my database CODE CREATE TABLE `businesses`
    (  `id` int(10) NOT NULL auto_increment,  `business` varchar(255) NOT NULL default '',
     `description` text NOT NULL,  `emailaddress` varchar(255) NOT NULL default '',  `website`
    varchar(255) NOT NULL default '',  PRIMARY KEY  (`id`) ) TYPE=MyISAM A....
  28. Chatbox / Guestbook
    No Database required (2)
    Okay, this is a simple script that lets your visitors enter in messages via a chatbox/guestbook. You
    can use this how you like. Adjust it if need be. It's very simple, so there is much room for
    improvement. Enjoy! CODE echo " "; echo "Name: "; echo "Message: "; echo " "; echo " ";
    CHATBOX.PHP if(isset($post)) {     if(file_exists('chatbox.txt'))     {          $fileId
    = fopen('chatbox.txt', 'a');          fputs($fileId, $_POST );        
     fputs($fileId, $_POST );          fclose($fileId);     }     else     {          $....
  29. Reversing The Order Of Items In A Database
    How do you do it? (4)
    I have a chat PHP script, that I need a little help with. I'm wanting to use PHP/MySQL so that
    it will show the most recent message first. What it's doing, is showing the oldest message
    first, and newest message last. Is there something I can do to reverse the order of the messages?
    Thanks. (I know...I've had to ask for PHP help often, but I can't quite get a PHP book
    right now...sorry)....
  30. About Set Permisstions....
    auto set it, how? (4)
    I have some problem about set permissions by automatic.. for example... I created txt file with
    php... but when my file was added to server(trap17), it always set to 644 and i want to set it to
    666 . Anybody help me... PLEASE /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" />....

    1. Looking for auto, pruning, sql, database, php,
Similar
Ms-access Database Question - Allowing Web Access to the Informaton
How Do I Connect To Live Database With Php Script? - while being hosted with ComputingHost
Delete Problem With Id - Id as auto_increment, and identifier for a row
Connecting Php Site To Database
Can Database Column Names Start With A Number? - Can database column names start with a number?
Read Xml And Import Into Database - question
Download Database Backup - Download Database Backup
Editing Information In A Mysql Database And Deleting Rows
Importing .csv Into Mysql Database - NEED HELP!
Php Search Engine Script For Mysql Database
Problem To Join Tables In Database - Problem in joining (many to many) relation
File In Database - Question.thanks
Code To Install Mysql Database Table?
Showing Numbers Of Mysql Entries In A Database Table - It should be easy, but I don't know how...
Requesting Auto Generating Id Tag In Php Code - Php Coding
Crontab Automated Database Backup - using crontab to make automated database
Suppressing Mysql Error In Php - How to suppress auto generated mysql err
Login / Authetication System Using Database - adding information
Can You Add Images Into A Mysql Database? - Using Php?
Is Doing The Whole Database / Php Thing Worth It? - Question from the inexperianced...
Database Extraction Layer - Does php has Database extraction layer?
Can Reset The Id Auto Increment? - phpMyadmin
Script That Deletes A Row In A Mysql Database Usin
Personal Blog - [ Database Required ]
Help Importing Mysql Database Results - Need help formatting link
Writing To Database Problem With Mysql - Not Writing Forum Post
Mysql Edit - editing info in a MYSQL database
Chatbox / Guestbook - No Database required
Reversing The Order Of Items In A Database - How do you do it?
About Set Permisstions.... - auto set it, how?

Searching Video's for auto, pruning, sql, database, php,
See Also,
advertisement


Auto Pruning An Sql Database With Php - How can i do this?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com