Professional Web Applications Themes

Catch 22 with file removal or mysql queries? - PHP Development

I'm probably missing something here. I want to write some code to: Delete the contents of a folder if that happens successfully carry out a mysql query Easy. However I also don't want to delete the FOLDER unless the query will/has run successfully. Which seems a catch 22. Is there a function to check if the syntax of the query is correct? Or a "dummy query you can perform to get a "yeh, that looks good respose" from MYSQL to eable me to have confidence in then running both requests? The concern is the state of the database getting out ...

  1. #1

    Default Catch 22 with file removal or mysql queries?

    I'm probably missing something here. I want to write some code to:

    Delete the contents of a folder

    if that happens successfully carry out a mysql query

    Easy.

    However I also don't want to delete the FOLDER unless the query will/has run
    successfully.

    Which seems a catch 22. Is there a function to check if the syntax of the
    query is correct? Or a "dummy query you can perform to get a "yeh, that
    looks good respose" from MYSQL to eable me to have confidence in then
    running both requests?

    The concern is the state of the database getting out of sync with the
    filesystem. Which must be common to every program / computer under the sun.

    TIA

    Bonge Boo! Guest

  2. #2

    Default Re: Catch 22 with file removal or mysql queries?

    Bonge Boo! wrote:
    > I'm probably missing something here. I want to write some code to:
    >
    > Delete the contents of a folder
    >
    > if that happens successfully carry out a mysql query
    >
    > Easy.
    >
    > However I also don't want to delete the FOLDER unless the query
    > will/has run successfully.
    >
    > Which seems a catch 22. Is there a function to check if the syntax of
    > the query is correct? Or a "dummy query you can perform to get a
    > "yeh, that looks good respose" from MYSQL to eable me to have
    > confidence in then running both requests?
    >
    > The concern is the state of the database getting out of sync with the
    > filesystem. Which must be common to every program / computer under
    > the sun.
    >
    > TIA
    Can't you just delete the contents, then run the query and if the result is
    ok delete the containing folder ? Or is that not what you are after ? I'd
    rather have a second removal action than two times a db access where only
    one is required.
    HTH
    Pjotr


    Pjotr Wedersteers Guest

  3. #3

    Default Re: Catch 22 with file removal or mysql queries?

    When you execute a mysql_query or mysql_db_query you get a result
    handle returned--let's call it $qhandle for now. Now, if the query
    failed (i.e. syntax error), $qhandle would be empty:

    $qhandle = mysql_query("SLEECT * FROM mytable");

    if (empty($qhandle)) // Failed; syntax error or the like
    die(mysql_error()); // Would complain about SLEECT

    Now, let's take the next example in mind (let's assume mytable is an
    empty table):

    $qhandle = mysql_query("SELECT * from mytable");

    if (empty($qhandle)) // Did not fail; correct syntax or the like.
    die(mysql_error());

    while ($row = mysql_fetch_assoc($qhandle)) {
    print_r($row); // Prints nothing
    }

    Our syntax was good, the query executed flawlessly, but we still got
    nothing. The best way to deal with this situation is to do another
    check, but this time on the returned results:

    $qhandle = mysql_query("SELECT * from mytable");

    if (empty($qhandle)) // *yawn*
    die(mysql_error());

    if (mysql_num_rows($qhandle) > 0) {
    while ($row = mysql_fetch_assoc($qhandle)) {
    print_r($row);
    }
    else {
    echo "No data to display";
    }

    So, getting back to your situation... You can use unlink() on every
    file in your directory and then use the example above to determine if
    you need to delete your base directory or not.

    I have a function I built to read every file/directory below a base
    directory (similar to a du -a on Linux), and you can use it if you
    want:

    function readdir_r($directory)
    {
    global $FILELIST;
    if (!is_dir($directory))
    {
    $message = "$directory is not a directory";
    trigger_error($message, E_USER_ERROR);
    }
    if (!file_exists($directory) || !is_readable($directory))
    {
    $message = "$directory does not exist, or permission denied";
    trigger_error($message, E_USER_WARNING);
    return;
    }
    if (substr($directory, -1) !== "/")
    {
    $directory .= "/";
    }
    $FILELIST[] = $directory;
    $dhandle = opendir($directory);

    while (false !== ($content = readdir($dhandle)))
    {
    if (!($content == "." || $content == ".."))
    {
    clearstatcache();
    $content = $directory . $content;
    if (is_dir($content))
    {
    readdir_r($content);
    }
    else
    {
    $FILELIST[] = $content;
    }
    }
    }

    closedir($dhandle);
    return true;
    }

    You have to add "global $FILELIST;" at the top of your php file.

    On Sat, 10 Jul 2004 12:38:18 +0100, Bonge Boo! <bingbongspamcop.net>
    wrote:
    >I'm probably missing something here. I want to write some code to:
    >
    >Delete the contents of a folder
    >
    >if that happens successfully carry out a mysql query
    >
    >Easy.
    >
    >However I also don't want to delete the FOLDER unless the query will/has run
    >successfully.
    >
    >Which seems a catch 22. Is there a function to check if the syntax of the
    >query is correct? Or a "dummy query you can perform to get a "yeh, that
    >looks good respose" from MYSQL to eable me to have confidence in then
    >running both requests?
    >
    >The concern is the state of the database getting out of sync with the
    >filesystem. Which must be common to every program / computer under the sun.
    >
    >TIA
    eclipsboi Guest

Similar Threads

  1. Question Needing help with MySQL queries.
    By Unregistered in forum Brainstorming Area
    Replies: 0
    Last Post: November 25th, 03:15 PM
  2. Caching Queries with CF MX 6.1 and MySQL 4.1
    By bluespoon in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: May 20th, 08:09 PM
  3. Replies: 4
    Last Post: November 9th, 05:51 AM
  4. Quick removal of the begging of a file?
    By Benjamin Goldberg in forum PERL Miscellaneous
    Replies: 8
    Last Post: August 22nd, 01:20 AM
  5. File Attachment Removal
    By Bob in forum Windows Setup, Administration & Security
    Replies: 1
    Last Post: July 7th, 04:21 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139