Thursday, October 7, 2010

Search and replace of exact matching string in PHP

If you have to make and exact search, use preg_replace function with simple regular expression pattern. This function is used to search and replace a string according to the pattern provided.
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
mixed = any type of data, from  string to array.
array to array replace, string to string replace.
If your pattern doesn't contain regular expression, and you are going to search and replace string, please use str_replace function instead.
$replacement is the new string that will substitute the string or array following the pattern.
$subject is the array or string in which search and replace operation performs.

To make exact match, you have to use \b in both sides of the pattern.
For example, 
$pattern="/\bpatternstring\b/"; or
$pattern="/\b[0-9a-zA-Z]+\b/";
If you have to escape any special characters, then use \ (backslash) before that character.
$pattern="/\bxyz\[\]pqrs\b/";
If you have to make the search irrespective of case, need to use i , if in your subject includes utf-8 characters, use u,in the pattern.
 $pattern="/\bxyz\[\]pqrs\b/iu";

Sunday, October 3, 2010

Best way to easily import heavy amount of data in MySQL database?

Dear friends,

We usually use phpmyadmin tool to do all types of database related operations. But, sometimes when we have large sql dump which PhpMyadmin failed to execute due to upload size and execution time limit of PHP script.
Then either you have to use MySQL GUI tools like SQLyog, Navicat or MySQL query browser or use MySQL command line.
Best way to export the DB is below:

E:\MySQL\MySQL Server 5.1\bin>mysqldump -u jroot -pjoomla1 -h hostname dbname > e:\backupfile.sql

If we want to export only table structure, execute below command:
mysqldump -ujroot -pjoomla1 -hlocalhsot --no-data prodtest  > e:\MySqlBkups\19112013jprod.sql
If we want to export only data of tables in a database, execute below command:
mysqldump -ujroot -pjoomla1 -hlocalhost --no-create-info prodtest  > e:\MySqlBkups\19112013jprod_data.sql
If we want to ignore a table during export a database, execute below command:
mysqldump -ujroot -pjoomla1 -hlocalhost jprodtest --ignore-table=jprod.sessions > e:\MySqlBkups\19112013jprod_data.sql

There are 3 ways to import the data using command line.
  1. The LOAD DATA statement
  2. The source Command
  3. The mysql command
Second way, source command is the best way among these. Below is the way to import :
  • First, you should logged-in to the MySQL and put the sql file wherever you want. To login on mysql command prompt, you have to go to the inner directory of mysql. For example,                                    c:>cd wamp/bin/mysql/mysql5.0.45/bin
  • A) c:/wamp/bin/mysql/mysql5.0.45/bin>mysql -u root -p***** -hipaddressofhost
    • mysql> source <path>filename.sql;
  • B) C:\wamp\bin\mysql\mysql5.5.24\bin>mysql -u <username> -p<password>  -D<dbname> <c:\filetoimport.sql
Note:  there should not be any space between -p and password. It will be same for -h and hostname. Space will be between -u and username.
These are 3 simple steps to execute a sql dump file. It will take very less time to execute heavy queries.




Feel free to comment on my post, if you find any difficulty to implement.

How to include an external file in Joomla?

Dear friends,

In some cases, you need to add an external file beside joomla file system in a module and component. In this case, your external file can't use the joomla libraries easily. Following is the way to use the external file.
Add following 10 lines of code on top of your external file.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
/* If you need to use database object, add following line too */
$db=JFactory::getDBO(); // use this $db object for database related operation



 Now, use this file as normal joomla file. You can include the joomla libraries  files as per the requirement also.

Feel free to provide your comment.

Important queries based formulas to watch performance of a MySQL based application

(1) Session Vs global status variables value select G.variable_name,G.variable_value GlobalValue,S.variable_value SessionValue from (selec...