Jump to content

mysql delete


blueman378

Recommended Posts

hi guys well im trying to write a code which orders all results in a table by the "time" column and if there is more than 20 it will delete the oldest one i have this so far

$q = "SELECT * FROM latest
          ORDER BY `time` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 20 ){
      mysql_query("DELETE FROM latest");
    }

 

but im kinda stuck as how to tell it to delete the result with the oldest time

 

cheers matt

Link to comment
Share on other sites

try :


$q = "SELECT * FROM latest ORDER BY `time` ASC";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_num_rows($result);
    if( $num_rows == 20 ){
      mysql_query("DELETE FROM latest");
    }

Link to comment
Share on other sites

well thats not what i mean i mean i need to extend

$q = "SELECT * FROM latest ORDER BY `time` ASC";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_num_rows($result);
    if( $num_rows == 20 ){
      mysql_query("DELETE FROM latest WHERE !!!HERE!!!");
    }

 

to make it delete the oldest results

 

cheers

Link to comment
Share on other sites

well that could work buit the data is inserted into the table with 2 columns one is simply name the other is time which is something like 020220080156 sort thing which is 2nd of the 2nd 2008 at 1:56 am ...

 

Inventing your own method of storing datetime is your mistake. Use a real datetime stamp and your problems will evaporate.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.