Posts

Showing posts from 2013

mysql tips - HAVING - another clause use to filter result like WHERE

Example: correct usage: SELECT c1, count(*) from t1 where Date like "2013-12-30 %" group by c1 HAVING count(*)>2 order by c1; wrong usage: SELECT c1, count(*) from t1 where Date like "2013-12-30 %" and count(*)>2  group by c1 order by c1; Because the aggregate function can not be used in WHERE clause. WHERE clause is used to SELECT the rows. And aggregate functions are after WHERE, and before HAVING. HAVING clause is used to filter the select result.

Google Analytics track in ruby on Rails application

There are several ways/language  to track your website. Here we discuss how to use it is ruby project. Solution 1: In Ruby, you can put the script below in the specific webpage to track only that page: rubyproject/app/views/webpagename/show.html.haml file :javascript   var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");   document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); :javascript   try{     var pageTracker = _gat._getTracker("$GOOGLE_ANALYTICS_ID");     pageTracker._trackPageview();   } catch(err) {} Solution 2: If you want to track all your webpages,  Step 1 : create a new haml file in the path: app/views/shared/_google_analytics.html.haml -For the local usage, development environment, in this _google_analytics.html.haml file:  :javascript   var _gaq = _gaq ...

mysql tips - INSERT ... SELECT..., a complex usage with "SELECT" a join table

Mysql can be as powerful as possible to process the data in all your tables, you just need to be patient to figure it out the way to combine all the functions/cmds together. Recently, I have a chance to experience this. This is base on the MYSQL cmd INSERT ... SELECT ... ON DUPLICATE KEY UPDATE usage, and it is possible that the SELECT part can be as complex as possible. INSERT t3name (DateTime, t3c1, t3c2, t3c3, t3data ) SELECT CONCAT(date(t1.Date),' ',hour(t1.Date),':00:00') AS DateTime, t1.c1, t1.c2, t1.c3,                     count(t1.c4) AS  t3data  FROM table2name t2        INNER JOIN table1name t1 on t2.c2=t1.c3 and t2.c3=t1.4        WHERE t1.Date like "2013-12-10 %" and t2.Date like "2013-12-10 %"        GROUP BY date(t1.Date), hour(t1.Date), t1.c2, t1.c3, t1.c4        ORDER BY t1.c2  ON DUPLICATE KEY UPDATE t3name. t3data = ...

postgreSQL usage

Basic usage comparing to mysql mysql:  SHOW TABLES postgresql:  \d postgresql:  SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'; mysql:  SHOW DATABASES postgresql:  \l postgresql:  SELECT datname FROM pg_database; mysql:  SHOW COLUMNS postgresql:  \d   table postgresql:  SELECT column_name FROM information_schema.columns WHERE table_name =' table '; mysql:  DESCRIBE TABLE postgresql:  \d+   table postgresql:  SELECT column_name FROM information_schema.columns WHERE table_name =' table '; Join usage Join 2 tables: SELECT      A.pka,      A.c1,      B.pkb,      B.c2 FROM      A INNER JOIN B ON A .pka = B.fka; Join 3 tables : t1 and t3 merger to t2: SELECT t1.column1, t2.column2, t3.column3 FROM   table1name t1 INNER JOI...

mysql connection related to EUR € symbol using PHP

"€" can not be recognized by  ISO-8859-1 standard as it is encoded by  UTF-8. By default, mysql connection using  ISO-8859-1 based connection, and will turn  "€"  as " â " using mysql query.    In order to get a correct "€", then mysql connection should be like this: $conn = mysql_connect($host.':'.$port, $username, $password); mysql_set_charset("utf8"); $query = "select ..."; $db= mysql_select_db($database, $conn); $result = mysql_query($query);

PHP usage tips

1.  Instance usage  Array overwritten after a loop Error code: $att = new Attachment(); for ($i=2; $i<=$parts;$i++){ $att->attachmentHeader = imap_bodystruct($imap, $msgno, $i); $att->attachmentContent = imap_fetchbody($imap, $msgno, $i); $attachments[$i-2] = $att;  } return $attachments; All the objects in the $attachments actually refer to one instance, and this instance object is overwritten by the last $att value. That is because the instance is defined outside the for loop, and every time when the $att is updated, and the $att is updated.  Correct code: for ($i=2; $i<=$parts;$i++){ $att = new Attachment(); $att->attachmentHeader = imap_bodystruct($imap, $msgno, $i); $att->attachmentContent = imap_fetchbody($imap, $msgno, $i); $attachments[$i-2] = $att;  } return $attachments; 2. Be careful of "space" When you define a string and set the value of the string, it is most possible that you will typo a "space" there, t...

email notification setting in ubuntu using postfix

Usage  example:  mail -s 'Error Report' username @example .com < /tmp/message .txt First, on a system have no mail cmd. "mailutils" needs to be installed. sudo apt-get install mailutils I use the postfic-mailx solution.  Install process: sudo apt-get install postfix After the installation is started, some windows will pop up and you need to select options as below: 1. Internet Site 2. Modify the server name when sending an email. You can of course use the default value. Configuration process: sudo dpkg-reconfigure postfix then need to select options as below for the configuration:  Internet Site mail .example.com change the path if needed mail .example.com , localhost.localdomain, localhost No 127.0.0.0/8  [::ffff:127.0.0.0]/104 [::1]/128  192.168.0.0/24 0 + all detail guideline here: https://help.ubuntu.com/11.10/ serverguide/postfix.html And if postfix is already installed by other...

log4php Failed connecting to mysql cluster database & could not find driver

My log4php gets this error in a new server environment. PHP Warning: log4php: [LoggerAppenderPDO:default]: Failed connecting to database. Closing appender. Error: could not find driver in ../log4php/LoggerAppender.php on line 283 I have the same settings for log4php (1. config.xml file 2.same log4php version) as my local development, which works perfectly. I tried simply log to a file in the new server, it works, which means the log4php lib has no problem. And I can even log to the database in the new server from my local script using local log4php lib, which means my server can recognize log4php logging. Reasons: After some searches and tests(script using PDO, not mysqli to connect to the database, good reference  here  and wiki ) , I confirm the problem is caused by lacking of pdo_mysql related driver. As log4php uses PDO extensions in PHP to  log to mysql database. Then PDO extension is needed. As PDO is defaultly included from PHP5.1, and our mysql-cluster ...

Regular expression live website and PHP regex trick with backslash

Regular expression is really help to process data. But a tiny typo can hardly be aware and that is why a "live" regex is really helpful for newbie as me when learning it. Below is a list of live regex for different coding language. PHP,   phpliveregex . An important note, if you use "\\_" in php to get "\_", which means escape the first "\", you actually need "\\\\_" in the regex in PHP. Ruby,  rubular

PHPUnit introduction and possible warnings/errors

1.  Beginner guidebook I am using Ubuntu 12.04, so below may only available for 12.04 and can have issues when using different unix system. 2.  Installation Cmd below will install the latest PHPUnit. And you need install "pear" first. >sudo pear install -- alldeps -- force phpunit / phpunit 3. Modify the php.ini First, find the php path: > pear config-show | grep php_dir                                   PEAR directory                 php_dir          /usr/share/php Then find  the php.ini file >locate php.ini    And change the include_path in php.ini to the value of php_dir: include_path = ".:/usr/share/php"                                 4. The output of the PHPUnit result >phpunit path/to/file/ClassName...

Security warning in MYSQL 5.6

Changes from MySQL 5.6.5: Security Fix: MySQL client programs now issue a warning if a password is given on the command line that this can be insecure. So, if you use a plain-text password/argument in the script like below: shell>mysql --user=${myuser} --password=${mypasswd} --port=${myport} --host=${myhost} ${mydb} Then you will get this warning: Warning: Using a password on the command line interface can be insecure. Solution: Suggestions from MYSQL bug forum . A better solution is to use mysql_config_editor which MYSQL officially suggested to add the called "mysqlpath" object which generated crypted credential for the mysql database. It is in the bin folder in your installed MYSQL5.6 folder. 1. To find the "mysql_config_editor" cmd, you can use: shell> which mysql_config_editor My case, it is in the path:  /usr/local/mysql-advanced-5.6.11-linux-glibc2.5-x86_64/bin/mysql_config_editor Note: if the "which" shows no result, it means...

Build a php+java+mysql project in Linux system

1. Check out the code from a version control system. From svn: svn co svn+ssh://user@127.0.0.1/filepath/trunk localpath note: keep the structure in the version control system. 2. Install php, java, mysql, and php-mysql, php5-curl For the mysql 5.6 installation. You can check on my other blog  Install MYSQL 5.6 in ubuntu 12.04 and set password If you get error as below: Fatal error: Call to undefined function mysql_connect() It is most possible that your php-mysql is not correctly installed. And needs to be installed: shell> sudo apt - get install php5 - mysql If you get error as below: PHP Notice: Use of undefined constant CURLOPT_RETURNTRANSFER It is because you do not install php5-curl, and you can install it like this: shell> sudo apt-get install php5-curl 3. set the java lib path set the $CLASSPATH for java project, there are three ways to set the value. solution 1: ~$ export CLASSPATH=$CLASSPATH:/path/to/external/lib:/path/to/external/jar/file/exam...

Include other projects in a new PHP project in eclipse

Include other projects correctly After you create a new php project, even you use the right path in the php code to include other project/classes, these classes/methods can not be selected/clicked using "shift + left click". That is because you need to set the setting of the new project, and select the include path by: Project properties->PHP Include Path->Projects, then add necessary projects.

SVN tips in Linux: create root repository, system-delete, conflict and partial checkout

1. create the  folder in the repository: (1) Create root repository. svn mkdir svn+ssh://username@hostname/projectname/trunk -m "Create the trunk folder" Then the first version can be checked out. (2) Create sub-folder in the root repository svn mkdir svn+ssh://username@hostname/projectname/trunk/test -m "Create the test folder" And this new folder needs to be checked out. Even you already created a folder using the same name in the local copy. Your local folder and files will not be overwritten.  And then you can commit your local files in the new folder into the repository. 2. Delete the svn local copy As the .svn folder in the checkout is an hide recursive folders, when you directly remove the folder, you will get the "protected file " warning and it is not easy to delete all the files in the folder. Below is the way to completely delete the folder. $cd file/with/.svn $find -name .svn -exec rm -r -f {} +  /* delete the recursive folders in t...

bash shell date usage (normal in bash and for bash parameter)

date function in bash shell script is really powerful. Notice:  1. no space after the "+" to define the format. 2. If use on a bash cron job cmd, then "\" is need to escape %. It recognize the datetime in many different format and can be convert to almost any format you need. 1. change YYYYMMDD to YYYY-MM-DD olddate="20121010" newdate=$(date -d"${olddate}" +%Y-%m-%d) 2. want to get the date n days before a fixed date olddate="20121010" days=n newdate=$(date -d"${olddate} -  ${days}  days" +%Y-%m-%d) 3. want to get the date n days after a fixed date olddate="20121010" days=n newdate=$(date -d"${olddate} +  ${days}  days" +%Y-%m-%d) 4. n month before newmonth=`date +%Y%m -d  "$n months ago"` 5. Use as the bash parameters Be aware that the "%" needs to be escaped ~$bash example.sh  `date +\%Y-\%m-\%d -d "3 days ago"` or ~$bash example.sh  $(date +\%Y-\%m-...

Install MYSQL 5.6 in ubuntu 12.04/14.04 and set password and remote access

Part 1 : The steps to install mysql 5.6 in ubuntu 12.04 Note: it is also possible that , when you install mysql, there is a pop-up window to ask you to enter the password for root, and you/'d better remember  that in case there will be some extra work to set password in the Part 2. 1. check the system OS version uname -i 2.If there is an old mysql version running on your system and you need to uninstall it, and if you also install a .deb version, you need to use the "dpkg" one to uninstall: sudo apt-get remove mysql-server mysql-server-5.5 mysql-server-core-5.5 sudo apt-get remove --purge mysql-server mysql-client mysql- common sudo dpkg --pure mysql-server mysql-client mysql-common sudo apt-get autoremove sudo apt-get autoclean deluser mysql sudo updatedb ##sometimes after above cmds,using "locate mysql" still lists many file related to mysql, that is because the system database is not updated## sudo find /var/lib/mysql/ -type f -delete ##delete ...