MySQL Database Cloning Tutorial

Well, when you are working on a project and you have a team on same project and you might be using git or any other SCM, then you know you will need new DB everytime there is a new product request , new bug, new improvement … i do it all the time when i am starting off a new clone or git branch.

The important question how you are going to do database cloning ? .. if you just go by dumping and importing .. it’s little boring for me… so i cooked up this simple script that does it for you….

I think it’s simple script that does it easily … here you go ..

#!/bin/bash

if [ -n "$1" ]; then
  YOUR_DB_NAME="$1" 
else
  echo "Error : Some Friendly error message . " 
  exit;
fi 
if [ -n "$2" ] ; then
  YOUR_DB_CLONE_NAME="$2" 
else
  YOUR_DB_CLONE_NAME="$YOUR_DB_NAME-clone" 
  echo "Hey this name is what i am gonna use $YOUR_DB_NAME"
fi
echo "Starting ..." 
sleep 1 
echo "creating new Database --> '$YOUR_DB_CLONE_NAME'"
mysqladmin create ${YOUR_DB_CLONE_NAME} -u root --password=tcial
echo "Cloning  '$YOUR_DB_NAME'  --> '$YOUR_DB_CLONE_NAME'"
sleep 1
mysqldump -u root --password=tcial ${YOUR_DB_NAME} | mysql -u root --password=tcial -h localhost ${YOUR_DB_CLONE_NAME}
sleep 1
echo "... Done "

Ask me any questions below in comments area.

Escaping String for MySQL and HTML with PHP

MySQL is a great RDBMS to begin with.I would like to discuss something that happens almost every now and then with people trying to save and retrieve HTML from MySQL database.
Let’s say you have a form which has a HTML input coming in, you need to be careful about two things :
1) Escaping potentially harmful values
2) retrieving HTML values usable within or outside quotes, like within value attribute of another tag

Well escaping part is well laid out with PHP’s mysql_real_escape_string() , take a look at what it does – “Escapes special characters in a string for use in an SQL statement”. Perfect , this is what we going to use when saving HTML to DB. But what if we have quotes and HTML tags within this string which we intend to use for the purpose we laid out in point two above. If that wasn’t the purpose we could just have used stripslashes to retrieve values back, no harm done. With our motive in mind i will use two function:

a) mysql_real_escape_string()
b) htmlentities

Here how it goes:

makeandExecuteQuery($value);
}
?>

and here’s how you will retrieve it




You see how we didn’t convert quotes back but did convert others with this easy to use flexible function that PHP provides.
Go Lookup reference if this doesn’t server your purpose: html_entity_decode

Tell me how you tried and what results you got…. did it fit in and serve you well 🙂

Joomla getBuffer tutorial

Joomla is something i like and don’t like for various reason but nevertheless i would like to guide you over a very useful feature joomla provides when you have your own modules/components doing some functionality in addition to joomla.

Say, you are using custom library which provides data or stores data and performs various operations in backend. Now say you have encountered some random error or perhaps an information that you would like your beloved user to know about or as feedback from system (well specified in Human-Computer Interaction guidelines).

Lets assume you have a function that saves some information in your own custom DB(not joomla DB) and you want user to know on next page that information was saved successfully.

public function letssave($yourparams="default value"){
//lets get the global app object 
global $mainframe;

//lets call random hypothetical functions
$this->_processUserInput(JRequest::getVar('user_info'));
   if($this->_inputProcessed()=='SUCCESS'){
       $mainframe->enqueueMessage("User your input has been processed successfully");
       $this->_nextProcessing();
       //return to next page
       $mainframe->redirect($return);
   }
   else {
       $mainframe->enqueueMessage("User your input has Failed for unknown reason");
       $this->_failSafeProcessing();
       //return to next/previous page
       $mainframe->redirect($return);
   }

}

Now you have all the code you need to enqueue your feedback messages using mainframe/Application object

Here’s how you can easily retrieve on your pages.

$doc =& JFactory::getDocument();
$buffer = $doc->getBuffer('message');
if($buffer){
  // put some HTML code in here
?>
?

Thats it. Now your users are well informed and upto date and there are no communication gaps between functionality and system feedback

Let me know how it goes for you….