+ Reply to Thread
Results 1 to 6 of 6

Thread: Help with "INTO OUTFILE"

  1. #1
    eliasthecrimson is offline x10Hosting Member eliasthecrimson is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    8

    Post Help with "INTO OUTFILE"

    I'm trying write a code snippet that saves a specific table in my database as a CSV file. The purpose of this is to automatically back up that specific table just before changes are made do it (that I can access through the File Manager and use to restore if needed). Some googling led me to using the INTO OUTFILE statement.

    This is what I'm trying to do:

    Code:
    /*
    ** Write tables to CSV file
    */
    $backup_query = "SELECT * FROM stock INTO OUTFILE 'stock_$today.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '\"' LINES TERMINATED BY '\n'";
    $backup = mysql_query($backup_query, $db);
    if($backup)
        echo "Backup OK";
    else 
        echo "Backup failed";
    The backup always fails and no file is written. I might be doing this completely wrong; any help is appreciated!
    Last edited by eliasthecrimson; 05-18-2011 at 10:55 AM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help with "INTO OUTFILE"

    Are you doing this on your computer or on x10hosting?

    The command runs on the MySQL server, which may or may not be the server where you account is located.

    Even then, you have to give the full path to the file, ie '/home/igor/public_html/backups/stock-table-dumpMay182011.txt'
    Nothing is always absolutely so.

  3. #3
    eliasthecrimson is offline x10Hosting Member eliasthecrimson is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    8

    Re: Help with "INTO OUTFILE"

    Doing this on the MySQL server on x10hosting (free). My MYSQL database server is "localhost", which is why I assumed this would work.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help with "INTO OUTFILE"

    Not sure if they are keeping the MySQL server on or off the machine for your accounts.

    1. You would still need the full path to the backup file, not just a relative path.
    2. Use mysql_error to test for error messages.
    Nothing is always absolutely so.

  5. #5
    eliasthecrimson is offline x10Hosting Member eliasthecrimson is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    8

    Re: Help with "INTO OUTFILE"

    Alright, thanks. Here is the code as it looks now:

    Code:
    /*
    ** Write tables to CSV file
    */
    $backup_query = "SELECT * FROM stock INTO OUTFILE '/public_html/restricted/merchants/backup/stock_$today.csv' FIELDS TERMINATED BY ';' ENCLOSED BY '\"' LINES TERMINATED BY '\n'";
    $backup = mysql_query($backup_query, $db) or die('Error: ' . mysql_error());
    ... and here's the error it gives when run:

    Code:
    Error: Access denied for user '*********'@'10.33.248.%' (using password: YES)
    Last edited by eliasthecrimson; 05-18-2011 at 04:28 PM.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Help with "INTO OUTFILE"

    The INTO OUTFILE clause requires the FILE privilege, which can't be granted to any user you create through cPanel since it can only be given globally.
    Last edited by misson; 05-21-2011 at 05:25 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 101
    Last Post: 11-04-2011, 02:43 AM
  2. Replies: 2
    Last Post: 05-19-2011, 02:07 AM
  3. Create "Free Wordpress Themes" section.
    By farscapeone in forum Off Topic
    Replies: 0
    Last Post: 05-17-2011, 04:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers