+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: mySql : Select into file

  1. #1
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    91

    mySql : Select into file

    Hello

    I want to make a copy of a table into a file in order to make a backup.
    PHP Code:
    $db  db_name;
    $dbh = new PDO("mysql:host=localhost;dbname=$db"'user''pw');
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    $TB db_name.table_name;
    $backupFile '../Backup/' $TB '.sql';
    $query "SELECT * FROM "$TB " INTO OUTFILE '".$backupFile."' ";
    $result mysql_query($query) or die ("pb sur le ".$query); 
    and that does not work ! It says :
    pb sur le SELECT * FROM db_name.table_name INTO OUTFILE '../Backup/db_name.table_name.sql'
    I don't understand what happens ! And the file is not created.

    Thank you for help

    The directory ../Backup exists before.

  2. #2
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: mySql : Select into file

    Quote Originally Posted by fomalhaut View Post
    PHP Code:
    $TB db_name.table_name;
    $backupFile '../Backup/' $TB '.sql';
    $query "SELECT * FROM "$TB " INTO OUTFILE '".$backupFile."' ";
    $result mysql_query($query) or die ("pb sur le ".$query); 
    The directory ../Backup exists before.
    It may be out of order:
    PHP Code:
    $string '';
    $tableName  'tableName';
    $backupFile 'backupFile';
    try
        
    $query =$dbh->query("SELECT * INTO OUTFILE '$backupFile' FROM $tableName");
    catch(
    PDOException $e
        print(
    "Error: ".$e->getMessage()."\n"); 
    Also try reading Using PHP to Backup MySQL Databases. Although, I am not sure of the format in which the information is shown on the page.

    Or you could use the fwrite() function to create a new file if it does not exist or empty and write to it if it does exist.
    PHP Code:
    $errors = array();
    $tableName  'tableName';
    $backupFile 'backupFile';
    $query =$dbh->query("SELECT * FROM tableName");
    if(
    $query->columncount() === 0)
       
    $errors[] = "No rows in table";
    if(!
    $query)
        
    $errors[] = "Query Error: ".mysql_error();
    if(
    sizeof($errors) == 0) {
      while(
    $row=$query->fetch()) {
       
    $column1 $row['0'];
       
    $column2 $row['1'];
       
    $column3 $row['2'];

    // Etc.

    // Format any way you wish
    $string .= "$column1 $column2 $column3"// Etc.
    }

    $fp fopen("$backupFile","w"); // the W parameter truncates the file, use A if you want to apply it to the end of the file which DOES NOT truncate the file.
    fwrite($fp,$string);
    fclose($fp);
    } else {
     echo 
    "<ul><li>";
      
    implode("</li><li>",$errors);
     echo 
    $errors."</li>";
    echo 
    "</ul>";

    Also if you use PDO, instead of the original:

    PHP Code:
    $query "SELECT fields FROM tableName";
    $result mysql_query($query); 
    You can use the PDO version:

    PHP Code:
    $query $dbh->prepare("SELECT fields FROM tableName");
    $query->execute(); 
    Refer to Writing PDO in PHP & MySQL
    Last edited by as4s1n; 03-24-2010 at 09:03 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  3. #3
    zenadix is offline x10Hosting Member zenadix is an unknown quantity at this point
    Join Date
    Mar 2010
    Posts
    13

    Re: mySql : Select into file

    Your error message is in french. Why?

  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: mySql : Select into file

    Dumping of SQL databases is not available on X10 hosting judging from the tests I have tried. I would suggest PHPMyBackupPro as a better alternative that does essentially the same thing.

  5. #5
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: mySql : Select into file

    Dumping really wasn't my intent, that was just a bonus for formalhaut.

    @formalhaut: BTW is this like a scheduled backup thing or just an occasional backup you want just in case?
    Last edited by as4s1n; 03-24-2010 at 05:07 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  6. #6
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    91

    Re: mySql : Select into file

    Hello.

    Quote Originally Posted by zenadix
    Your error message is in french. Why?
    Of course, there are, I'm french !

    Quote Originally Posted by as4sIn
    BTW is this like a scheduled backup thing or just an occasional backup you want just in case?
    You're right, that's just an occasional backup, because, the tables will not move a lot.
    Please, what the exact meaning of "BTW" ?

    In facts:
    Having changed some things, I've now a more precise message:
    Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1086 File '../Backup/db_name.table_name.sql' already exists'
    And alas, I don't find it in any directory !!!!

    I'm searching !

  7. #7
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: mySql : Select into file

    'BTW' means 'By The Way'.

    If it says it already exists, I'm not quite sure what to tell you, you looked through the folder? You could always try the fwrite method I suggested, which writes to a file whether or not it exists, which would probably mean the server thinks it has that but doesn't. I have noticed before that the cPanel file manager does not like certain names, although that may be just me. try 'db_tableName.sql', It's worth a shot.

    One more question: Are you going to input the file directly into the database (import) or copy the data in it to the table?
    Last edited by as4s1n; 03-25-2010 at 10:14 AM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  8. #8
    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: mySql : Select into file

    Quote Originally Posted by fomalhaut View Post

    And alas, I don't find it in any directory !!!!

    I'm searching !
    Try using absolute paths. Not sure if the mySQL server treats '../Backup/db_name.table_name.sql' as relative to the server rather than to the script.
    Nothing is always absolutely so.

  9. #9
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    91

    Re: mySql : Select into file

    OK, I've found !! Select Into file writes in a directory relative to the mysql directory. And that doesn't arrange me because this is not the same place on my test server than on the x10 server.

    So I've tried your "fwrite method", as4s1n. And it works, putting the file where I expect it.

    although... I've an other pb that I'm searching for: each column is written twice in my file !

    I've no time now, but if I don't found, I will explain you. Probably an idioty of my small mind !

    Thanks again for having help me.

  10. #10
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: mySql : Select into file

    Is the duplicate columns result from the fwrite method or the OUTPUT clause in the query?
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [PHP/MySql] Sorting After Select
    By iamcameron in forum Programming Help
    Replies: 3
    Last Post: 01-13-2010, 11:57 AM
  2. MySQL SELECT Query question
    By stevet70 in forum Programming Help
    Replies: 2
    Last Post: 06-17-2009, 10:44 AM
  3. PHP / MySQL SELECT problem
    By stevet70 in forum Programming Help
    Replies: 3
    Last Post: 01-21-2009, 07:38 AM
  4. [php][mysql]Select From issues.
    By taekwondokid42 in forum Programming Help
    Replies: 3
    Last Post: 03-01-2008, 07:34 AM
  5. MySQL SELECT Question
    By Synkc in forum Programming Help
    Replies: 3
    Last Post: 02-09-2008, 03:51 PM

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