Mac OS X – Enable memcached on MAMP

I had to install memcached on Mac OSX El Captain and MAMP and it was quite tricky. Let’s do it together to make your life more easier.

0. Preparation

The easiest way is to use homebrew to install it. If you do not have brew already, run command in your terminal.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

1. Install memcached

To install memcached, run this command in your terminal.

brew install memcached

Most of the time, everything goes well and that’s the reason why a lot of articles finishes here. I had an issue yesterday with libevent package. But I have removed and install it again and the issue was solved.

brew remove --force libevent
brew install libevent

2. Install memcached as PHP extension

This quite depends on the PHP version you are using with the MAMP. To find all the supported php memcached version, run this command in the terminal:

brew search memcached

I use PHP 7.0 on localhost, so I have to install php70-memcached. But as I said, this depends on the PHP version, you are running on localhost.

brew install homebrew/php/php70-memcached

For some reason I was force to run this command, so maybe you will need to run it as well. But it’s also part of the message / help written in terminal, so it’s good idea to read them 😉

brew install --HEAD homebrew/php/php70-memcached

Btw, this command takes a while to finish, but now we should have memcached installed on Mac OS X.

php -i | grep memcached
...
memcached support => enabled
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
...

3. Create memcached.so and update php.ini
Now the memcached is installed for our PHP Cli version (command line version of PHP), but it might not be accessible for PHP running in browser. If you didn’t know there’re 2 version of PHP, it’s fine ;). You learned another new thing.

3a. Download memcached.so from Github
I found a lot of articles describing how to build memcached.so from source codes of PHP, but actually it never worked for me. Most probably I was doing some silly mistakes, but at the end, I found Github repository with already done memcached.so files which are ready to be downloaded and used.

https://github.com/majksner/php-memcached-mamp

I would recommend you to do the same – download it and use it. It will save you a lot of time 😉

3b. Copy memcached.so to proper extension folder
As I said earlier, I use PHP 7.0 on my localhost, so the folder with extension for me is

/Applications/MAMP/bin/php/php7.0.0/lib/php/extensions/no-debug-non-zts-20151012

So move the file to the proper folder.

3c. Edit php.ini and add info about new extension to be loaded

Edit proper php.ini file

vim /Applications/MAMP/bin/php/php7.0.0/conf/php.ini

And add

extension=memcached.so

Somewhere in this file. It’s good idea to find where extension= are and add it as the last one.

3c. Restart MAMP

The last thing to be done is to restart MAMP and check whether everything is working as expected.

If you run http://localhost:8888/MAMP/index.php?language=English&page=phpinfo in your browser, you should find memcached section on this page. If yes, congratulation – memcached is installed on your MAMP and you can take advantage of it.

4. Congratulation

I do not know better feeling than when the things are finally working as expected. So CONGRATULATION, memcached is up and running on our Mac OS X El Captain and also in MAMP.

Please let me know if you had any other issues while installing mamcached on your Mac OS X using MAMP, so we can solve them together.

MySQL – How to find out hour difference between two dates

My challenge for yesterday was to find out, how many hours a user is registered. So in other words, what’s hour difference between the date/time an user register and now.

And believe or not, it’s pretty easy.

# Created is saved as unixtime, e.g. 1455251945
SELECT 
  FLOOR((now() - u.created) / 3600) hours
FROM
  users u
WHERE 
  uid = 1;

If you save created as datetime, you can still do it.

# Created is saved as datetime, e.g. 2016-02-11 11:00:12
SELECT 
  FLOOR((now() - UNIX_TIMESTAMP(u.created)) / 3600) hours
FROM
  users u
WHERE 
  uid = 1;

So challenge done, job done and smile kept 😉

Mac OS X – Set up autocomplete hotkey in MySQLWorkbench

I use MySQLWorkbench everyday and the autocomplete hotkey is not enabled by default. I can’t figure out why, but luckily, there is a way how to enable it.

1. Edit /Applications/MySQLWorkbench.app/…/main_menu.xml

vim /Applications/MySQLWorkbench.app/Contents/Resources/data/main_menu.xml

2. Find section Auto-complete

You can use /Auto-complete in vim to find the string

3. Remove comments

<value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.code-completion-menu">
  <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
  <value type="string" key="caption">Code Completion</value>
  <value type="string" key="name">cc-menu</value>
  <value type="string" key="itemType">cascade</value>
  <value type="string" key="context">*query</value>

  <value type="list" content-type="object" content-struct-name="app.MenuItem" key="subItems">
    <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.list-members">
      <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
      <value type="string" key="caption">Auto-complete</value>
      <value type="string" key="name">list-members</value>
      <value type="string" key="command">builtin:list-members</value>
      <value type="string" key="itemType">action</value>
      <value type="string" key="shortcut">Modifier+Space</value>
      <value type="string" key="platform">windows,linux</value>
      <value type="string" key="context">*query</value>
    </value>
    <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.edit.list-members-mac">
      <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.edit</link>
      <value type="string" key="caption">Auto-complete</value>
      <value type="string" key="name">list-members</value>
      <value type="string" key="command">builtin:list-members</value>
      <value type="string" key="itemType">action</value>
      <value type="string" key="shortcut">Command+Escape</value>
      <value type="string" key="platform">macosx</value>
      <value type="string" key="context">*query</value>
    </value>
  </value>
</value>

4. Change hot-key in case you like another one.

<value type="string" key="shortcut">Command+Escape</value>
<value type="string" key="platform">macosx</value>

5. Restart MySQL Workbench

Restart MySQL Workbench and enjoy your Code completion / Autocomplete feature.

Regular expression – exclude string from the url

My friend Hubert came to me yesterday to find solution for his issue:

Write a regular expression for all the urls from a specific domain, but skip those which contain /admin-web/ string.

It took me some time, as I do not like negation in regular expression, but we found out the correct regular expression:

^http(s)?:\/\/(www\.)?pavlatka\.(com|cz|sk)\/(?!admin-web\/?.*$).*$

You can test it in one of the greatest Regular expression tool out there http://regexr.com/3coen.

Let me know if you have any other solution how to achieve this.

MySQL – How to convert string into date data type

I’ve been working on the project after someone and the programmer didn’t like the different data types in the database. That’s only explanation for me because he saved everything into the VARCHAR data type – also dates. So instead of using DATE, DATETIME or TIMESTAMP data types, he just put everything into VARCHAR and it was done. So any SQL query using DATE functions are quite difficult. Also instead of saving dates in YYYY-mm-dd HH:ii:ss format (at least), he preferred to save it in mm-dd-YY-HH-ii format.

Please, if you can think of why he would do so, please let me know.

Fortunately MySQL comes with STR_TO_DATE function, which is very helpful in this case.

SELECT created FROM users WHERE uid = 10; // Result: 1-8-2015-11-20

Let’s use STR_TO_DATE function of MySQL

SELECT STR_TO_DATE(created, '%Y-%m-%d %H:%i:%s') FROM users WHERE uid = 10; // Result: 2015-08-01 11:20:00

And because the string was converted into date format, we can start using all DATE functions the MySQL offers to us.

It’s not clever to use public transport in Berlin without ticket

Yes that’s right – it’s not clever to use public transport in Berlin without ticket. The people responsible for checking it are very active here in Berlin and the chance they will check you is quite high.

I remember back home in Czech Republic – it was pretty rare and I used it pretty much, but it doesn’t apply to Berlin at all. I’ve been here for little bit more a month and I’ve got checked 3 times already and saw another 2 checking before I entered the vehicle.

And with the information that a fine is 60 euro I would not recommend to anyone to try his/her luck. I know that 80 euro per month is pretty much, but you will be able to travel in peace.

I love to work after someone

Time to time I have a privilege to work after someone else. So it’s an opportunity for me to see how other people do their coding. And most of the time I am surprised.

Recently I found out this code:

if($variable == 'A') {
   // .. do certain action .. //
}

if($variable == 'B') {
   // .. do certain action .. //
}

if(!($variable == 'A' && $variable == 'B')) {
   // .. do certain action .. //
}

Not only, that the system must check 2 extra conditions each time the program runs, but even when we is sure, that it has to do something for the rest of the options (apart A and B), still he has to verify, that the condition is met.

This code would be much more better:

if($variable == 'A') {
   // .. do certain action .. //
} elseif($variable == 'B') {
   // .. do certain action .. //
} else {
   // .. do certain action .. //
}

What about switch statement?

Some of you might suggest to use the switch statement instead and I would agree, but only in certain situation. I prefer / recommend to use switch only if the action for each case is simple and can be written to up to 3 lines of code. And doesn’t contain any other if statements.

switch($variable) {
   case 'A':
      // .. do certain action ..//
      break;
   case 'B':
      // .. do certain action ..//
      break;
   default:
      // .. do certain action ..//
      break;
}

Xdebug, PHPStorm and MAMP Set up

I was trying to find a simple manual how to enable xdebug in PHPStorm on OSX and MAMP and I found this video. It’s not only about how to set it up, but it will also show you how to use it correctly.

It helped me to get everything set up and I believe it might help you as well.

Great job.

Prepare coins when traveling by tram in Berlin

We have decided to go to Ikea to make our very first furniture shopping in Berlin. We found the way online, dress kids and were ready to go.

When we came to the tram stop, we got quite confused but we met a lady speaking English who helped us out. I asked her where I should buy the tickets and she told me that I do not need to worry that I can buy them inside the tram.

That brought peace to my heart. How surprised I was when I came to the ticket machine and I found out, that it accepts only coins. So the next stop we got out and I went to find some shop to change my money to coins. That was not as easy as I looks as it was on Sunday and all the shops are closed. But I managed eventually and we could continue on our journey to ikea.

So if you need to buy ticket in a tram, break you pig and take coins with you.

Check your LastPass setting before moving to another country

I’ve been using LastPass for more than a year now and I am more than happy. I do not need to remember any other password just the main one.

Last week I moved from Cyprus to Germany and I faced the issue I never thought of. I couldn’t login to my to my LastPass account. And from the fact I wrote above that meant I couldn’t login anywhere. The reason was simple – in my security setting I allowed only access from Cyprus.I was quite desperate so I contacted my friend who still lives in Cyprus to help me out. But unfortunately he was busy and I needed it urgently. But few things were on my side.

I have an access to my email only mobile via wifi and the second email I attached to my account has a forwarder to my main email address.

So when I tried to login to my account, LastPass simply informed me that Germany is not among allowed countries but also sent me an email with a link where I can ask to add it there. The issue is that this email is sent to your secondary email address, but luckily I use it only as a forwarder to my mail account. So I got this mail to my mobile as well and I could allow access from Germany as well.

So my issue was solved and I can use my LastPass as usual.

But my advice is – check you LastPass security settings before you mov to another country. It will help you from unexpected surprises.

© 2016 Tomas Pavlatka

Theme by Anders NorenUp ↑