Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

export and import by php

There are at least three ways to backup your MySQL Database :

Execute a database backup query from PHP file.
Run mysqldump using system() function.
Use phpMyAdmin to do the backup.


Execute a database backup query from PHP file

Below is an example of using SELECT INTO OUTFILE query for creating table backup :

<?php
include 'config.php';
include 'opendb.php';

$tableName = 'mypet';
$backupFile = 'backup/mypet.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);


include 'closedb.php';
?>
To restore the backup you just need to run LOAD DATA INFILE query like this :

<?php
include 'config.php';
include 'opendb.php';

$tableName = 'mypet';
$backupFile = 'mypet.sql';
$query = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);


include 'closedb.php';
?>
It's a good idea to name the backup file as tablename.sql so you'll know from which table the backup file is


Run mysqldump using system() function

Default Sort Order For Rails models

// provide a default sort order in case an order by clause isn't defined in the find clause
// Place this code somewhere in your model's class file.

   1  
   2          def self.find(*args)
   3            order_arg = args.collect do |arg|
   4              if arg.kind_of? Hash 
   5                if arg.keys[0] == :order
   6                  arg
   7                end
   8              end
   9            end
  10  
  11            if order_arg.compact.empty?
  12              args << {:order=>"place order by clause here e.g. 'name asc'"}
  13            end
  14            
  15            super
  16          end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS