Sunday, June 5, 2011

mysql database dump using php

Sometimes application needs database backup or rather dump on a regular interval or on request.

So we need a faster and easy way to create mysql dump and also compress those database files to save disk space.

 

This could be a very easy if we use some shell commands through php and create mysql dump. See below how we could achieve this.

 

 

<?php

//Database details

$dbhost = "localhost";

$dbname = "bear44_visitusa";

$dbuser = "bear44_visit";

$dbpwd  = "H8JKjfhyur&*HGrjy";

 

 

 

//List of tables you want to dump separated by single space

$tbl = "getaways_leftmenu getaways_seourl getaways_cmspages";

 

//execute command

exec("mysqldump -h $dbhost -u $dbuser -p\"$dbpwd\" $dbname $tbl > sqlfilename.sql");

 

 

//stores location of current directory

$dir = dirname(realpath(__FILE__));

 

 

 

//command to compress previously created sql file, also gzip -9 states to remove original sql file 

$zip = "gzip -9 $dir/sqlfilename.sql";

 

//execute command

exec($zip);

 

?>
   

 

thats it and you have zipped version of your dump file and the original sql file is automatically removed

 

 

Saturday, June 4, 2011

how to validate hexadecimal color code in php

When taking Hex color code as an input from user it is necessary to validate it. We can validate using preg_match function in php which returns 1 if matches found else 0 using following code.
if(preg_match('/^#[a-f0-9]{6}$/i', $hex_color))
{
//color code is valid
....
}
else
{
//Invalid code
....
}