1/30/2013

International Standard Serial Number (ISSN) checker algorithm based on PHP

An International Standard Serial Number (ISSN) is a unique eight-digit number used to identify a print or electronic periodical publication.

Algorithm
1. Calculate the sum of the first seven digits of the ISSN multiplied by its position in the number, counting from the right — that is, 8, 7, 6, 5, 4, 3, and 2, respectively.

2.  The modulus 11 of this sum is then calculated: divide the sum by 11 and determine the remainder.

3. If there is no remainder the check digit is 0, otherwise the remainder value is subtracted from 11 to give the check digit: 11 -remainder

4. An upper case X in the check digit position indicates a check digit of 10.

5. To confirm the check digit, calculate the sum of all eight digits of the ISSN multiplied by its position in the number, counting from the right (if the check digit is X, then add 10 to the sum). The modulus 11 of the sum must be 0.

PHP Source Code

$issn = '03785955';
if($issn == checker($issn)){
echo 'good';
}else{
echo 'wrong';
}

function checker($issn=0,$chechsum = 0) {              
        $issn = substr($issn, 0, 7);
     
        foreach (str_split($issn) as $key => $value) {
            $chechsum += (8 - $key) * $value;
        }
     
        $chechsum = $chechsum % 11;
        if ($chechsum) {
            $chechsum = 11 - $chechsum;
            if ($chechsum == 10) {
                $chechsum = 'X';
            }
        }      

        return $issn.$chechsum;
    }

or One improved performance algorithms



function checker($issn = 0, $chechsum = 0) {
        $issn = substr(preg_replace('/[^0-9]+/', '', $issn), 0, 7);

        if (strlen($issn) == 7) {
            foreach (array_reverse(str_split((int) $issn)) as $key => $value) {
                if ($value) {
                    $key = $key + 2;
                    $chechsum = $key * $value + $chechsum;
                }
            }

            $chechsum %=11;
            if ($chechsum) {
                $chechsum = 11 - $chechsum;
                if ($chechsum == 10) {
                    $chechsum = 'X';
                }
            }

            return $issn . $chechsum;
        } else {
            return '';
        }
    }

1/21/2013

Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'


$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT, 
          $LOCAL_DB_USER, 
          $LOCAL_DB_PASS, 
          array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES 'UTF8'")
      );

Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' 

In fact, the MYSQL_ATTR_INIT_COMMAND is 1002. But at some times the php can not recognize it. The one reason is that you have not installed php-mysql for php_module.

If you use yum to install php and mysql, then you may use

sudo yum install php-mysql

sudo service httpd restart

or you may compile your php with the --with-pdo-mysql option


That is OK. Now you may access the constant MYSQL_ATTR_INIT_COMMAND. Good luck to you.

1/20/2013

centos install mysql5.5 by yum

When you want to use yum to use mysql5.5, you should let yum know the repos. Now I give one way for you to install mysql5.5 on your centos 5 or centos 6.

1.  On centos 6
 //on 64 computer
 rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh  http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-10.ius.el6.noarch.rpm

 // on 86 computer
rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm

// if the file does not exist, please go to  http://ftp.riken.jp/Linux/fedora/epel//6/ to find the suitable version for epel-release-6-XXX.noarch.rpm

2.  On centos 5


 //on 64 computer
 rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//5/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh   http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-10.ius.el6.noarch.rpm

 // on 86 computer
rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//5/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm

// if the file does not exist, please go to  http://ftp.riken.jp/Linux/fedora/epel//5/ to find the suitable version for epel-release-5-XXX.noarch.rpm

Last please use

yum remove mysql*

yum install mysql55

1/19/2013

install php5.4 on centos 6

When you want to use yum to use php5.4, you should let yum know the repos. Now I give one way for you to install php5.4 on your centos 5 or centos 6.

1.  On centos 6
 //on 64 computer
 rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh  http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-10.ius.el6.noarch.rpm

 // on 86 computer
rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm

// if the file does not exist, please go to  http://ftp.riken.jp/Linux/fedora/epel//6/ to find the suitable version for epel-release-6-XXX.noarch.rpm

2.  On centos 5


 //on 64 computer
 rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//5/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh  http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-10.ius.el5.noarch.rpm

 // on 86 computer
rpm -Uvh http://ftp.riken.jp/Linux/fedora/epel//5/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/i386/ius-release-1.0-10.ius.el5.noarch.rpm

// if the file does not exist, please go to  http://ftp.riken.jp/Linux/fedora/epel//5/ to find the suitable version for epel-release-5-XXX.noarch.rpm

Last please use
yum install php54

1/15/2013

Three methods for switching between multiple databases by PDO

In general, when you create one PDO object, you have chosen one database based on your connection. E.g.

$con = new PDO($DSN,$user, $passwd);

But when you want to switch between multiple databases, what should you do? Now I give 3 methods for switching databases based on PDO objects.

All databases should be from or have same
  1.  server
  2. db engine
  3. user account
  4. password.

Or you only use the third method.

Example:
Databases
first_db and second_db

tables:
f_name in first_db;


+------+-------+
| id   | name  |
+------+-------+
|  100 | China |
|  200 | japan |
|  300 | USA   |
+------+-------+



s_name in second_db;


+------+-----------+
| id   | name      |
+------+-----------+
|  100 | Noda      |
|  200 | Matsumoto |
|  300 | Okamoto   |
+------+-----------+

initiate the pdo connection 
$conn = new PDO('mysql:dbname=first_db;host=127.0.0.1;port=3306','user','passwd');

$sql = 'select * from s_name ';
$conn->query($sql);

Methods for switching first_db to second_db,

1. use 'use db';    
    $conn->exec('use second_db');
    $sql = 'select * from s_name ';
    $conn->query($sql);

2. use database name
    $sql = 'select * from second_db.s_name ';
    $conn->query($sql);

3. Create new connection 
    $conn = new PDO('mysql:dbname=second_db;host=127.0.0.1;port=3306','user','passwd');
    $sql = 'select * from s_name ';
    $conn->query($sql);



phpMyAdmin Wrong permissions on configuration file

Today I met one error when I configure multiple host for phpMyAdmin on centos 6.3.  The error message was

   Wrong permissions on configuration file, should not be world writable!

Then I changed the permission of file [ config.inc.php] from 777 to 705. The problem was resolved. Nice, the login page comes out.

sudo chmod 705 ./phpMyAdmin/config.inc.php


1/14/2013

职场分为三个层次


我把职场分为三个层次,以次对应基本的三个楼层。

    1、管事

    就是做事,把自己的本质工作做好,做到尽可能好,做到比比人好,做到别人做不到。

    2、管人

    就是除了做好自己的事,还好把自己的人管好。只管人不做事不容易让人信服,只做事不管人是没有尽责,实质是退步。管人是很大的学问,让人信服,让人愿意听你的,让人高高兴兴的把事儿做了,这是你的本事。

    3、管钱

    到了三楼基本就是公司的高层了,那就不是简单管事管人了。比如一个项目来了,你第一考虑的这个项目需要多少资金,有多大风险,回款如何,会不会影响到公司的资金链。然后再去考虑这个项目如何实现,需要多少人,多长时间。最后考虑实现的细节。

    也就是要站在公司层面和角度上,系统化的解析项目,综合考虑,准确执行。

http://forum.eet-cn.com/BLOG_ARTICLE_15847.HTM?click_from=8800100458,9009455666,2013-01-14,EECOL,FORUM_ALERT&jumpto=view_welcomead_forum_1358133914650

1/09/2013

MySQL master+slave configure

For reference from MySQL, please go to http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-replication.html

1. install mysql on master and slave host
   master host: 192.168.10.98
   slave  host:  192.168.10.96

   -- create same database and table on master host and slave host.
   create database test_db;
   use test_db;
   create table test_table (id int(10), account char(10), passwd char(10));

2. configure master host
 
   -- edit my.cnf
   ## Replication
   server-id                       = 1001
    log-bin                         = mysql-bin

   You may set server-id from 1 to 2^23 -1 freely

   -- grant slave host
    grant replication slave on *.* to 'slave96'@'192.168.10.96' identified by 'slave9696';

   --flush database
   FLUSH TABLES WITH READ lOCK;

   -- show master status
    sudo mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |     1837 |              |                  |
+------------------+----------+--------------+------------------+

Please remember the file and position value

  -- unlock
   UNLOCK TABLES;

3. configure slave host


-- edit my.cnf
   ## Replication
   server-id                       = 1002
    log-bin                         = mysql-bin

-- change the master

change master to
master_host=’192.168.10.98’,
master_user=’slave96’,
master_password=’slave9696’,
master_log_file=’mysql-bin.000001’,
master_log_pos=1837;

--start slave
 start slave

--check slave statuss
show slave status;


*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.98
                  Master_User: slave96
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2167
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 2144
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
----------

In the show above, the Slave_IO_Running and Slave_SQL_Running must be YES. 

Till now, you have completed the configure for master-slave. Please try this functionality.
Insert one record on master host
INSERT INTO test_table VALUES(100,'Jone', 'Jone100');

You may check the slave host. There must be one same record on slave host.