Mysql – Calculate database size

You can calculate MySql database size using following steps.

Login into mysql :

mysql -h hostname -u username -p

Above command will ask you to enter mysql password.
After log in into mysql, run following query.

SELECT table_schema "DatabaseName",
sum( data_length + index_length ) / 1024 / 1024
"DataBase Size in MB" FROM information_schema.TABLES
GROUP BY table_schema;

Just replace your Database name. Thats it.
Please leave your comments if it worked for you.

Install eclipse in ubuntu-linux

Install Eclipse

Type the following command to install extensible Tool Platform, Java IDE, Java Development Tools plug-ins for Eclipse:
$ sudo apt-get install eclipse eclipse-jdt

How to start eclipse?

Click on Applications > Programming > Eclipse

OR type the following command at shell prompt:
$ eclipse &

Done. Enjoy

WordPress – Use php variables in javascript function

In WordPress , sometimes you need to use your PHP variable into included javascript file. You can use below function for this.

wp_register_script( 'myscript', FB_LIKE_PLUGIN_URL . 'fb.js' , array('jquery'), '2.5.4.6' );
wp_enqueue_script( 'myscript' );
$local_variables = array('app_id' => $app_id,'name' => "Milap");
wp_localize_script( 'myscript', 'vars', $local_variables );

wp_register_script function include my own created javascript file from plugin directory and also include jquery file.
$local_variables contains array in which you have to pass all variables with key which you want to access into your fb.js file.
wp_localize_script function will pass this array into js file & you can access theses variables in javascript file like vars.app_id and vars.name !!

Let me know , if you have any confusion ..

Google +1 Button using Javascript

The easiest method for including google +1 button on your web page is to include the necessary JavaScript resource and to add a +1 button tag.

Just add below tag into your HTML where you want to put google +1 button.

<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone"></div>

Add below java script into your head section.

<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

That’s it.
You can find all other parameters at https://developers.google.com/+/plugins/+1button/

Facebook login using javascript

<!DOCTYPE html>
<html xmlns:fb="https://www.facebook.com/2008/fbml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>

    <body>
        <div id="fb-root"></div>
        <script type="text/javascript">
            var button;
            var userInfo;

            window.fbAsyncInit = function () {
                FB.init({
                    appId: 'YOUR_APP_ID', //change your app id
                    status: true,
                    cookie: true,
                    xfbml: true,
                    oauth: true
                });

                function updateButton(response) {
                    button = document.getElementById('fb-auth');
                    userInfo = document.getElementById('user-info');

                    if (response.authResponse) {
                        //user is already logged in and connected
                        FB.api('/me', function (info) {
                            login(response, info);
                        });

                        button.onclick = function () {
                            FB.logout(function (response) {
                                logout(response);
                            });
                        };
                    } else {
                        //user is not connected to your app or logged out
                        button.innerHTML = 'Login';
                        button.onclick = function () {

                            FB.login(function (response) {
                                if (response.authResponse) {
                                    FB.api('/me', function (info) {
                                        login(response, info);
                                    });
                                } else {
                                    //user cancelled login or did not grant authorization

                                }
                            }, {
                                scope: 'email,user_birthday,status_update,publish_stream,user_about_me'
                            });
                        }
                    }
                }

                // run once with current status and whenever the status changes
                FB.getLoginStatus(updateButton);
                FB.Event.subscribe('auth.statusChange', updateButton);
            };
            (function () {
                var e = document.createElement('script');
                e.async = true;
                e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
                document.getElementById('fb-root').appendChild(e);
            }());

            function login(response, info) {
                console.log(info);
                if (response.authResponse) {
                    var accessToken = response.authResponse.accessToken;

                    userInfo.innerHTML = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + "<br /> Access Token: " + accessToken + "<br /> Your Birthday:" + info.birthday + "<br /> Your Email:" + info.email + "<br /> Your First Name:" + info.first_name + "<br /> Your Last Name:" + info.last_name + "<br /> Your Gender:" + info.gender + "<br /> Profile Link:" + info.link + "<br /> Username:" + info.username;

                    button.innerHTML = 'Logout';

                    document.getElementById('other').style.display = "block";
                }
            }

            function logout(response) {
                userInfo.innerHTML = "";
                document.getElementById('debug').innerHTML = "";
                document.getElementById('other').style.display = "none";

            }

            //stream publish method
            function streamPublish(name, description, hrefTitle, hrefLink, userPrompt) {

                FB.ui({
                    method: 'stream.publish',
                    message: '',
                    attachment: {
                        name: name,
                        caption: '',
                        description: (description),
                        href: hrefLink
                    },
                    action_links: [{
                        text: hrefTitle,
                        href: hrefLink
                    }],
                    user_prompt_message: userPrompt
                },

                function (response) {

                });

            }
        </script>
        <button id="fb-auth">Login</button>
        <br />
        <div id="user-info"></div>
        <br />
        <div id="debug"></div>
        <div id="other"></div>
    </body>

</html>

Some OOP Facts

Some OOP concepts

  • The main difference between OOP and functional programming is that the data and code are bundled together into one entity, which is known as an object.
  • During the execution of an object’s method, a special variable called $this is automatically defined, which denotes a reference to the object itself.
  • if a parent class declares a member as public, the inheriting child class must also declare it as public. Otherwise, the child would not have an is-a relationship with the parent, which means that anything you can do with the parent can also be done with the child.
  • When no access modifier is given for a method, public is used as the default.
  • You can define any number of methods as abstract, but once at least one method of a class is defined as abstract, the entire class needs to be declared as abstract, too.
  • In PHP, a method is either abstract (without code) or it’s fully defined.
  • PHP chose interfaces as an alternative to multiple inheritance.
  • An interface is declared similar to a class but only includes function prototypes (without implementation) and constants.
  • Note: Interfaces are always considered to be public; therefore, you can’t specify access modifiers for the method prototypes in the interface’s declaration.
  • There are times where you might want to make sure that a method cannot be re-implemented in its derived classes. For this purpose, PHP supports the final access modifier for methods that declares the method as the final version, which can’t be overridden.

How MySQL Uses Indexes

Some MySQL index Facts

  • MySQL indexes are used to find rows with specific column values quickly.
  • Without an index, MySQL must begin with the first raw and then read through entire table to find rows.
  • If the table has an index for the columns, MySQL can quickly determine the position to seek into the middle of the data file without having a look at all data.
  • If a table has 1000 raws, this will 100 times faster than reading sequentially.
  • Most MySQL indexes are stored in B-Tree.
  • The database stores the index value as a B-Tree key, and the record pointer as a B-Tree value.
  • Whenever you search for a record holding a certain value of an indexed column, the engine locates the key holding this value in the B-Tree, retrieves the pointer to the record and fetches the record.
  • What exactly is a “record pointer”, depends on the storage engine.
    • In MyISAM, the record pointer is an offset to the record in the MYI file.
    • In InnoDB, the record pointer is the value of the PRIMARY KEY.
  • In InnoDB, the table itself is a B-Tree with a PRIMARY KEY as a B-Tree key. This is what called a “clustered index” or “index-organized table”. In this case, all other fields are stored as a B-Tree value.
  • In MyISAM, the records are stored without any special order. This is called “heap storage”.
  • MySQL uses indexes for these operations :
    • To find the rows matching a WHERE clause quickly.
    • To eliminate rows from consideration. If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows.
    • To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size.
    • To find the MIN() or MAX() value for a specific indexed column.

I found these useful information on http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html.