#PHP

Change MYSQL data directory

Sometimes you need change mysql data directory for the reason expand hard disk driver or rearrange your directory structure. This tutorial will show you the way to change it in Ubuntu.

For example, change mysql data directory from /var/lib/mysql to /var/data/mysql

Looking for:

Change to :

And open file:

Looking for:

Change to:

Reload AppArmor profile:

Finally, restart MYSQL:

It’s done.

Install Memcached, PHP Memcache and PHP Memcached in Ubuntu 14.04

As you know, Memcached is great cache system for your web application. Use correctly Memcached will speed up your application so much. So now is how to install memcached, PHP Memcache and PHP Memcached

Install Memcached, PHP Memcache and PHP Memcached in Ubuntu 14.04

In Ubuntu 14.04, the installation is very simple, just need one command:

Please remember restart apache2:

Finally, test your works by writing some PHP scripts.

Add php script to test.php:

Successful result:

 

 

Different between MyISAM and InnoDB

MyISAM means MYSQL Indexed Sequential Access Method. Basically, It is a flat file, and is very old technology. But do not let that put you off using it. Because a flat file means it is not relational, and thus is not an RDBMS, and thus is more appropriate for some situations.
InnoDB is the full RDBMS like almost one you know.  InnoDB is great for if you have a lot of relational links (with references, joins, etc).

With MyISAM, It implements locking on the table, and while that lock is held, no other session can perform a SELECT. With InnoDB, It implements locking on the row.

Additional thing, MyISAM supports full-text search.

Both MyISAM and InnoDB are good. But when you design database you need determine if you need features provided by InnoDB. If not, then MyISAM is up for consideration.

 

PHP create a class with dynamic variables

PHP provides overloading methods to help you create dynamically properties and methods. In this case we will use 2 functions __set() and get(). The below is the concept of  that two functions.

__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.

Now, we will write a class with dynamic variables using __get(), set() functions.

If you do not know about magic functions in PHP, this issue will is not easy for you. But, if you know it, it is so simple.

Difference between Static Method and Dynamic Method

The easiest way to understand is we should start from looking at our code to know how to we used Static Method and Dynamic Method,

With Static Method:

With Dynamic Method:

So you will easily to recognize that Static Methods are usually used in static library. For example, you create a class to handle string as cuting string, uppercase … And based on the script above,  we see that Dynamic Method just is used after creating a instance of object.

You do not need to concern to performance when choose Static or Dynamic Method, just focus to software design. You can use Static Method when you need and choose Dynamic Method when you need instantiate your class.

Install MongoDB and Mongo PHP in Ubuntu

I am using Ubuntu Server 14.04 OS for my working enviroment. All steps below is way I implemented successfully on my machine. So now, I will show you how to install MongoDB and Mongo PHP driver.

530df3ee-2908-11e4-beda-12313d275d6d-medium

Firstly, install MongoDB. You can see it is so simple and one and only one command.

To make sure that MongDB is running, you can use command below

To install Mongo PHP Driver, you use the command below.

The command above use Pecl, so if you machine have not installed it yet. You can install easily by the command below

And now, you have to enable Mongo PHP extension by way add “extension=mongo.so” to php.ini file or use the commands below

Almost done, just need to check that your job is done or not.

And writing a php script to create document, collection and insert data to mongodb.

If success, the output will be same as the below

 

Install PHP Redis in Ubuntu

Redis is good solution to improve the performance of your system. You can use Redis as cache system or nosql DB.  I think that is is easy to use it but not easy to think how and where.

Step 1,  install redis, phpize

Test redis:

Install phpize

Step 2,  download phpredis source code from git

If you have not installed git yet, you can use this command line:

Step 3, compile and install phpredis,

Step 3, enable phpredis extension

Step 4, test

Writing a php script to test

Doing some quick and dirty benchmark

 

Good luck,

PHP LinkedIn Oauth2 Example

The source code below implements 3 steps to get user information from LinkedIn.

Step 1: Start authorization process

Step 2: Get access token

Step 3: Fetch user information (user name, first name, last name …)