SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deleting from mysql error

    Hi Guys,

    i'm trying to delete old entries in the database but i'm getting a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result " when the code is ran.

    PHP Code:
    <?php
         $number_of_days 
    60;
         
    // using UNIX timestamp in the table.../////////////////////////////////////////////
          
         
    $stamp strtotime("-".$number_of_days." day");  
         
         
    // using MySQL DATETIME...//////////////////////////////////////////////////////////
         
    $stamp date("Y-m-d H:i:s"strtotime("-".$number_of_days." day"));
         
         
    $result mysql_query("SELECT * FROM `uploaded_files` WHERE `date_added`  <= ".$stamp);
         while (
    $row mysql_fetch_array($result)) {
     
         
    $file $row['file_name'];
     
         
    unlink("uploads/$file");
     
         
    // remove files...//////////////////////////////////////////////////////////////////
         
    }
         
         
    mysql_query("DELETE FROM `uploaded_files` WHERE `date_added ` <= ".$stamp); // remove entries from database. 
    ?>
    i can't seem to see the error!

    thanks for nay help guys

    Graham

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Echo the $samp, see if it's correct.

    Echo the queries before you execute them, see if they are correct.

    Use mysql_error() to catch the sql errors:

    Code php:
    $result = mysql_query($sql) or die (mysql_error());
    Saul

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Remove the extra space before the last back tick, and enclose your datetime in single quotes since you are representing it with a string.

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys,

    when executed this is the error i get:

    PHP Code:
    You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near '08:29:03' at line 1 
    also my "date added" field is entered into mysql as: 2007-06-05 21:05:43

    when i echoed $stamp i got : 2007-04-09 08:36:44

    @Dan i'm not to sure how exactly to remove the white space though?

    thanks for the help guys

    Graham

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That error is the result of not enclosing the value of $stamp in single quotes. You can remove the white space by clicking before the backtick with your mouse then pressing the backspace key

    PHP Code:
    $result mysql_query("SELECT * FROM `uploaded_files` WHERE `date_added`  <= '" $stamp "'"); 

    ...


    mysql_query("DELETE FROM `uploaded_files` WHERE `date_added` <= '" $stamp "'"); // remove entries from database. 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •