Sunday, August 19, 2012

MySQL - isnull usage


SELECT book.code, book.bookname, book.author, book.publisher, ifnull(cc.testtime,0)
------------------------------------------------------------------------------------------------------
SELECT book.code, book.bookname, book.author, book.publisher, if(cc.testtime is null,0,cc.testtime)
------------------------------------------------------------------------------------------------------
SELECT book.code, book.bookname, book.author, book.publisher, if(isnull(cc.testtime),0,cc.testtime)
------------------------------------------------------------------------------------------------------

 as borrow_times
FROM book LEFT JOIN (select loan.code as testcode ,count(loan.stuid) as testtime
from loan
group by loan.code)  AS cc ON book.code=cc.testcode
ORDER BY book.code;


===============================
Result:
===============================
codebooknameauthorpublisherborrow_times
c001HarryCharliecharlie2
c002The RingAndyTonli1
c003KonanDowaTonli0

Saturday, August 18, 2012

MySQL Performance - EXISTS , IN , Join

CPU : Pentium(R) Dual-Core CPU       T4400  @ 2.20GHz
RAM:1024 MB
OS:CentOS Linux release 6.0(2.6.32-71.el6.i686 #1 SMP)

Table Engine - MyIsam
BOOK_DBF ---> 66042 rows , 7224996 length
NUSALE_DBF ---> 9321 rows , 372824 length

*********************

mysql> select * from NUSALE_DBF
    -> where not exists(
    -> select * from BOOK_DBF
    -> where BOOK_DBF.BA = NUSALE_DBF.BA);

22 rows in set (40 min 47.75 sec)
............................................................

mysql> select * from NUSALE_DBF
    -> left join BOOK_DBF on BOOK_DBF.BA = NUSALE_DBF.BA
    -> where BOOK_DBF.BA is null;
22 rows in set (48 min 21.97 sec)
............................................................

mysql> select * from NUSALE_DBF
    -> where NUSALE_DBF.BA not in(
    -> select BOOK_DBF.BA from BOOK_DBF where BOOK_DBF.BA=NUSALE_DBF.BA);
22 rows in set (43 min 53.54 sec)
............................................................


+------------+-----------+---------------+------+------+
| SA         | NA        | BA            | SS   | SL   |
+------------+-----------+---------------+------+------+
| 2010-02-20 | 010101010 | 9789864192700 | N    |    1 |
| 2010-02-20 | 010101010 | 9789574828616 | N    |    1 |
| 2010-03-09 | 010101010 | 9789574823307 | N    |    1 |
| 2010-03-09 | 010101010 | 9789574823314 | N    |    1 |
| 2010-03-11 | 010101010 | 9789574823321 | N    |    1 |
| 2010-03-11 | 010101010 | 9789574823338 | N    |    1 |
| 2010-03-17 | 010101010 | 9789574827107 | N    |    1 |
| 2010-03-17 | 010101010 | 9789574823307 | N    |    1 |
| 2010-03-18 | 010101010 | 9789574823819 | N    |    1 |
| 2010-03-18 | 010101010 | 9789574823802 | N    |    1 |
| 2010-03-19 | 010101010 | 9789574823758 | N    |    1 |
| 2010-03-19 | 010101010 | 9789574823765 | N    |    1 |
| 2010-03-24 | 010101010 | 9789574826537 | N    |    1 |
| 2010-03-24 | 010101010 | 9789574823321 | N    |    1 |
| 2010-03-26 | 010101010 | 9789864190133 | N    |    1 |
| 2010-03-30 | 010101010 | 9789574823642 | N    |    1 |
| 2010-03-30 | 010101010 | 9789864190454 | N    |    1 |
| 2010-03-31 | 010101010 | 9789861580142 | N    |    1 |
| 2010-03-31 | 010101010 | 9789574823765 | N    |    1 |
| 2010-05-08 | 010101010 | 9789574823710 | N    |    1 |
| 2010-05-26 | 010101010 | 9789861583556 | N    |    1 |
| 2010-06-05 | 010101010 | 9789861582979 | N    |    1 |
+------------+-----------+---------------+------+------+




Friday, August 17, 2012

MySQL subquery - Difference in EXISTS NOT EXISTS, IN NOT IN


Table master_a
Table master
id
name
1
Charlie
2
David
3
Joomla
id
name
2
David
3
Andy



========================================
 (where master_a.name = master.name)
========================================
---------------------------------
USING Where Clause
---------------------------------
select * from master_a
where exists (
select * from master where master_a.name = master.name
);

---------------------------------
select * from master_a
where master_a.name in (
select master.name from master where master_a.name = master.name
);
---------------------------------
NOT USING Where Clause
---------------------------------
select * from master_a
where master_a.name in (
select master.name from master
);


---------------------------------
Result :
---------------------------------
idname
2David


PS. SAME AS>

select * from master_a left join master on master_a.name = master.name
where master.id is not NULL;

========================================
 (where master_a.idmaster.id)
========================================

select * from master_a
where exists(
select * from master where master_a.id = master.id
);

---------------------------------

select * from master_a
where master_a.id in (
select master.id from master where master_a.id = master.id
)


---------------------------------
Result :
---------------------------------

idname
2David
3Joomla


========================================

※ In,and Not In ---> Can be defferent condition
========================================
select * from master_a
where master_a.name in (
select master.name from master where master_a.id = master.id
);
---------------------------------
Result :
---------------------------------
idname
2David




========================================
※Wrong Usage ("exists,not exists" must have a "where" clause)
========================================
select * from master_a
where exists (
select * from master
)
idname
1Charlie
2David
3Joomla



===================================================

MySQL type (eng)

Ref. http://help.scibit.com/mascon/masconMySQL_Field_Types.html
MySQL Field Types
MySQL supports a number of column types, which may be grouped into three categories: numeric types, date and time types, and string (character) types. This section first gives an overview of the types available. Please refer to the MySQL manuals for more details.

Type Use for Size 
TINYINT A very small integer The signed range is 128 to 127. The unsigned range is 0 to 255. 
SMALLINT A small integer The signed range is 32768 to 32767. The unsigned range is 0 to 65535 
MEDIUMINT A medium-size integer The signed range is 8388608 to 8388607. The unsigned range is 0 to 16777215 
INT or INTEGER A normal-size integer The signed range is 2147483648 to 2147483647. The unsigned range is 0 to 4294967295 
BIGINT A large integer The signed range is 9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615 
FLOAT A small (single-precision) floating-point number. Cannot be unsigned Ranges are 3.402823466E+38 to 1.175494351E-38, 0 and 1.175494351E-38 to 3.402823466E+38. If the number of Decimals is not set or <= 24 it is a single-precision floating point number 
DOUBLE,
DOUBLE PRECISION,
REAL 
A normal-size (double-precision) floating-point number. Cannot be unsigned Ranges are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0 and 2.2250738585072014E-308 to 1.7976931348623157E+308. If the number of Decimals is not set or 25 <= Decimals <= 53 stands for a double-precision floating point number 
DECIMAL,
NUMERIC 
An unpacked floating-point number. Cannot be unsigned Behaves like a CHAR column: unpacked means the number is stored as a string, using one character for each digit of the value. The decimal point, and, for negative numbers, the- sign is not counted in Length. If Decimals is 0, values will have no decimal point or fractional part. The maximum range of DECIMAL values is the same as for DOUBLE, but the actual range for a given DECIMAL column may be constrained by the choice of Length and Decimals. If Decimals is left out its set to 0. If Length is left out its set to 10. Note that in MySQL 3.22 the Length includes the sign and the decimal point 
DATE A date The supported range is 1000-01-01 to 9999-12-31. MySQL displays DATE values in YYYY-MM-DD format 
DATETIME A date and time combination The supported range is 1000-01-01 00:00:00 to 9999-12-31 23:59:59. MySQL displays DATETIME values in YYYY-MM-DD HH:MM:SS format 
TIMESTAMP A timestamp The range is 1970-01-01 00:00:00 to sometime in the year 2037. MySQL displays TIMESTAMP values in YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD or YYMMDD format, depending on whether M is 14 (or missing), 12, 8 or 6, but allows you to assign values to TIMESTAMP columns using either strings or numbers. A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation because it is automatically set to the date and time of the most recent operation if you dont give it a value yourself 
TIME A time The range is -838:59:59 to 838:59:59. MySQL displays TIME values in HH:MM:SS format, but allows you to assign values to TIME columns using either strings or numbers 
YEAR A year in 2- or 4- digit formats (default is 4-digit) The allowable values are 1901 to 2155, and 0000 in the 4 year format and 1970-2069 if you use the 2 digit format (70-69). MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. (The YEAR type is new in MySQL 3.22.) 
CHAR A fixed-length string that is always right-padded with spaces to the specified length when stored The range of Length is 1 to 255 characters. Trailing spaces are removed when the value is retrieved. CHAR values are sorted and compared in case-insensitive fashion according to the default character set unless the BINARY keyword is given 
VARCHAR A variable-length string. Note: Trailing spaces are removed when the value is stored (this differs from the ANSI SQL specification) The range of Length is 1 to 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given 
TINYBLOB,
TINYTEXT 

A BLOB or TEXT column with a maximum length of 255 (2^8 - 1) characters 
BLOB,
TEXT 

A BLOB or TEXT column with a maximum length of 65535 (2^16 - 1) characters 
MEDIUMBLOB,
MEDIUMTEXT 

A BLOB or TEXT column with a maximum length of 16777215 (2^24 - 1) characters 
LONGBLOB,
LONGTEXT 

A BLOB or TEXT column with a maximum length of 4294967295 (2^32 - 1) characters 
ENUM An enumeration A string object that can have only one value, chosen from the list of values value1value2, ..., or NULL. An ENUM can have a maximum of 65535 distinct values. 
SET A set A string object that can have zero or more values, each of which must be chosen from the list of values value1value2, ... A SET can have a maximum of 64 members

MySQL datatype(cht)

Ref. http://ithelp.ithome.com.tw/question/10030564
1. 數值資料型態
有INTEGER, SMALLINT, DECIMAL, NUMERIC, FLOAT, REAL, DOUBLE PRECISION. 我們常用INT代替INTEGER, DEC代替DECIMAL, 以及DOUBLE代替DOUBLE PRECISION.
INTEGER有五個相關的型態:
TINYINT: 1個位元組, 可計數整數範圍-128~127, 如無正負號則為0~255
SMALLINT: 2個位元組, 可計數整數範圍-32768~32767, 如無正負號則為0~65535
MEDIUMINT:3個位元組, 可計數整數範圍-8388608~8388607, 如無正負號則為0~16777215
INT: 4個位元組, 可計數整數範圍-2147483648~2147483647, 如無正負號則為0~4294967295
BIGINT: 8個位元組, 可計數整數範圍-9223372036854775808~9223372036854775807, 如無正負號則為0~18446744073709551615
其他都是浮點數,
DECIMAL(M, D): 總計M位數, 小數點D位, 約佔用最多4位元組空間
NUMERIC(M, D): 總計M位數, 小數點D位, 約佔用最多4位元組空間
FLOAT, REAL, DOUBLE: 精度不同的浮點數, 在MySQL中也允許FLOAT(M, D)、REAL(M, D)或DOUBLE(M, D)的方式宣告, 但建議用DECIMAL、NUMERIC有較好的程式移植性, 佔用位元組空間方面, FLOAT(p): 如果P介於0到24間, 佔用4位元組, 介於25到53之間, 佔用8位元組; FLOAT佔用4位元組; REAL及DOUBLE佔用8位元組
另外還有一個BIT, 一個位元, 但MySQL容許BIT(M)的方式宣告, 即將M個位元構成一個資料欄項, 我們可以用來指定每個位置的存放位元為0或1, 這對一些Flag的應用是很方便的佔用的空間大約是(M+7)/8 bytes
2. 日期資料型態
DATETIME: 'YYYY-MM-DD HH:MI:SS', 佔用八個位元組
DATE: 'YYYY-MM-DD', 佔用3個位元組
TIMESTAMP: 'YYYY-MM-DD HH:MI:SS', 佔月4個位元組, 其內容是由系統自動給予
TIME: 'HH:MI:SS', 佔用3個位元組
YEAR: YYYY, 佔用一個位元組
3. 字元資料型態
CHAR(M): M個固定長度字元, M介於0到255之間
VARCHAR(M): M個變動長度字元, 如M介於0到255之間, 佔用M+1個位元組空間. 如M大於255, 則佔用M+2個位元組空間
BINARY(M): 佔用M個位元組, M介於0到255之間
VARBINARY(M): 如M介於0到255之間, 佔用M+1個位元組空間. 如M大於255, 則佔用M+2個位元組空間
BLOB, TEXT: 又有TINYBLOB, TINYTEXT, MEDIUMBLOB, MEDIUMTEXT, LONGBLOB, LONGTEXT等不同的宣告方式, 這部份, 我想等到討論到圖片檔案或Word、Excel等檔案如何存放在MySQL中時, 再詳細說明這幾個資料型態比較容易解釋
ENUM('值1','值2',...): 字序集合, 意即, '值1'是在位置0, '值2'在位置1...依此類推, 找出一個字串值在此集合中的位置, 佔用1到2個位元組, 在ENUM集合中可以放65536個元素
SET('值1','值2',...): 字串集合, 可以將一群相關的字串宣告成一個資料集合, 例如SET('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'), 最多能有64個元素, 視元素多少, 可能佔用1, 2, 3, 4或8個位元組

Friday, August 03, 2012

Linux vim auto complete

Most usefule:
Ctrl +x , Ctrl +f  <--- search file
Ctrl +x , Ctrl +p  <--- search keyword in the buffer and file
Ctrl +x , Ctrl +n  <--- search keyword in the buffer and file

just use the following during vim:
Ctrl +p
or
Ctrl +n

Linux YUM repo priority setting

To prevent several YUM Repo conflict. I found that I can use "yum-plugin-priorities" to prevent from that situation.

-------------------------------------------
yum install -y yum-plugin-priorities
[root@charlie yum.repos.d]# vim my.repo

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
enabled=1
priority=6

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=0
priority=2
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

MySQL UTF8 setting for PHP and MySQL Connection

Ref.http://www.vixual.net/blog/archives/310
Ref.http://blog.cmchen.net/2007/12/17/2498/

/etc/my.cnf

[mysql]
default-character-set=utf8

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid



mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql>

=======================================================

<?php
        //----------------mysql utf8----------------

        mb_internal_encoding('utf8');
        mysql_query("SET CHARACTER SET 'utf8'");






        mysql_query("SET NAMES 'UTF8;'");
        mysql_query("SET CHARACTER_SET_CLIENT=UTF8;");
        mysql_query("SET CHARACTER_SET_RESULTS=UTF8;");
        //----------------mysql utf8----------------
?>

Entire Database(mysql) search with PHP

Ref. http://fcontheweb.com/articles/search_database_with_PHP/




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search an entire database with PHP</title>

<style type="text/css">

table {
        width: 100%;
        border-collapse:collapse;
}

table, th, td {
        border: 1px solid black;
        padding: 3px;
}

a { color: #4D2078; PADDING-RIGHT: 2px; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; PADDING-TOP: 2px; background-color:#EEEBFF; height: 20px; width: 120px; text-align: center; ; border: #A498BD; border-style: outset; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px}
              a:hover { BORDER-RIGHT: ##605080 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: #605080 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; BORDER-LEFT: #605080 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: #605080 1px outset;background-color:#BDAAE2; height: 20px; width: 120px; text-align: center; }

</style>

</head>

<body>
<?php if (isset($_POST["search"]) && ($_POST["search"] != "")) { ?>
<a href="searchdb.php">Search Again !!</a>
<br>
<?php } ?>
<?php if (isset($_POST["search"]) && ($_POST["search"] != "")) {
$ori_search=$_POST["search"];
$highlight_search="<font color=\"red\">" . $_POST["search"] . "</font>";

        $conn = mysql_pconnect("localhost", $_POST["username"], $_POST["password"]) or die ("Error connecting to mysql");

        //----------------mysql utf8----------------
        mb_internal_encoding('utf8');
        mysql_query("SET CHARACTER SET 'utf8'");

        mysql_query("SET NAMES 'UTF8;'");
        mysql_query("SET CHARACTER_SET_CLIENT=UTF8;");
        mysql_query("SET CHARACTER_SET_RESULTS=UTF8;");
        //----------------mysql utf8----------------


        mysql_select_db($_POST["database"]);

        $record_exists="false";
        $aryTables = array();
        $aryFields = array();

        $sql = "SHOW TABLES FROM " . $_POST["database"];
        $result = mysql_query($sql);

        while ($row = mysql_fetch_row($result)) {
                $aryTables[sizeof($aryTables)] = $row[0];
        }

        for ($i = 0; $i < sizeof($aryTables); $i = $i + 1) {
                $sql = "SHOW COLUMNS FROM " . $aryTables[$i];
                $result = mysql_query($sql);
                while ($row = mysql_fetch_row($result)) {
                        $aryFields[sizeof($aryFields)] = $row[0];
                }

                $sql = "SELECT * FROM " . $aryTables[$i] . " WHERE ";
                for ($j = 0; $j < sizeof($aryFields); $j = $j + 1) {
                        $sql = $sql . $aryTables[$i] . "." . $aryFields[$j] . " LIKE '%" . $_POST["search"] . "%'";
                        if (($j + 1) != sizeof($aryFields)) {
                                $sql = $sql . " OR ";
                        } else {
                                $sql = $sql . ";";
                        }
                }

                $result = mysql_query($sql);
                if (mysql_num_rows($result) > 0) {
                        echo "<p>" . $aryTables[$i] . "</p>";
                        echo "<table><tr><thead>";
                        foreach ($aryFields as $field => $value) {
                                echo "<th>" . $value . "</th>";
                        }
                        while ($aryData = mysql_fetch_assoc($result)) {
                                echo "<tr>";
                                for ($j = 0; $j < sizeof($aryFields); $j = $j + 1) {
                                        echo "<td>" . str_replace($ori_search,$highlight_search,substr(htmlspecialchars($aryData[$aryFields[$j]], ENT_QUOTES), 0, 150)) . "</td>";
                                }
                                echo "</tr>";
                        }
                        echo "</table>";
                        $record_exists="true";
                }

                $aryFields = array();

        }
if ($record_exists == "false"){
                                header("location:searchdb.php?record_exists=1");
}

} else {?>
        <?php
        if (isset($_GET['record_exists']) && $_GET['record_exists'] == "1"){
                                        echo "<font color=\"red\">No Record Found !</font>";
        }
        ?>
        <form name="details" action="searchdb.php" method="POST">
        <p>Database: <input type="text" name="database" value="DBF2mySQL" /></p>
        <p>Username: <input type="text" name="username" value="root" /></p>
        <p>Password: <input type="password" name="password" value="charlie" /></p>
        <p>Search term: <input type="text" name="search" /></p>
        <p><input type="submit" value="Search" /> <input type="reset" value="Reset" /></p>
        </form>

<?php } ?>

<?php if (isset($conn)) {mysql_close($conn);} ?>

</body>
</html>




multi dbf files to mysql with php


<?php
/*
* Part 2 code modified by Clint Christopher Canada from different public domain sources
* Part 1 code created by Clint Christopher Canada. BSD licensed.
*/

function import_dbf($db, $table, $dbf_file)
{
global $conn;
if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
$num_rec = dbase_numrecords($dbf);
$num_fields = dbase_numfields($dbf);
$fields = array();

for ($i=1; $i<=$num_rec; $i++){
$row = @dbase_get_record_with_names($dbf,$i);
$q = "insert into $db.$table values (";
foreach ($row as $key => $val){
if ($key == 'deleted'){ continue; }
$q .= "'" . addslashes(trim(mb_convert_encoding($val,"UTF-8","BIG5"))) . "',"; // Code modified to trim out whitespaces (BIG5 to UTF-8)
}
if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
$q = mb_substr($q, 0, -1);
$q .= ')';
if (!$result = mysql_query($q, $conn)){
print (mysql_error() . " SQL: $q
\n");
print (substr_count($q, ',') + 1) . " Fields total.";
$problem_q = explode(',', $q);
$q1 = "desc $db.$table";
$result1 = mysql_query($q1, $conn);
$columns = array();
$i = 1;
while ($row1 = mysql_fetch_assoc($result1)){
$columns[$i] = $row1['Field'];
$i++;
}
$i = 1;
foreach ($problem_q as $pq){
print "$i column: {$columns[$i]} data: $pq
\n";
$i++;
}
die();
}
}
}

exec("ls |grep DBF",$a);
foreach ($a as $value){
$tbl=str_replace('.','_',$value);
echo $tbl."\n";


// This is Part I of the code
//$tbl = "book_dbf3";
$db_uname = 'root';
$db_passwd = 'charlie';
$db = 'DBF2mySQL';
$conn = mysql_pconnect('localhost',$db_uname, $db_passwd);

// Path to dbase file
$db_path = "./$value";

// Open dbase file
$dbh = dbase_open($db_path, 0)
or die("Error! Could not open dbase database file '$db_path'.");

// Get column information
$column_info = dbase_get_header_info($dbh);

// Display information
//print_r($column_info);

$line = array();

foreach($column_info as $col)
{
switch($col['type'])
{
case 'character':
$line[]= "`$col[name]` VARCHAR( $col[length] )";
break;
case 'number':
$line[]= "`$col[name]` FLOAT";
break;
case 'boolean':
$line[]= "`$col[name]` BOOL";
break;
case 'date':
$line[]= "`$col[name]` DATE";
break;
case 'memo':
$line[]= "`$col[name]` TEXT";
break;
}
}
$str = implode(",",$line);
$sql = "CREATE TABLE `$tbl` ( $str )DEFAULT CHARSET=utf8;";
//$sql = "CREATE TABLE `$tbl` ( $str )ENGINE=InnoDB DEFAULT CHARSET=utf8;";

//set db utf8
mb_internal_encoding('utf8');
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
//set db utf8

mysql_select_db($db,$conn);

mysql_query($sql,$conn);
set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.

// This is part 2 of the code

import_dbf($db, $tbl, $db_path);

}
?>

MySQL backup best way!

Ref. http://crazyrushstar.blogspot.tw/2009/05/mysqlibdata1.html
mysqldump -u root -p --lock-all-tables --add-drop-database > temp.sql

Thursday, August 02, 2012

Import DBF(dbaseIII) to mySQL with php (Solve BIG5 to UTF-8)

Ref http://www.ostalks.com/2009/12/31/import-dbf-files-to-mysql-using-php/


<?php
/*
* Part 2 code modified by Clint Christopher Canada from different public domain sources
* Part 1 code created by Clint Christopher Canada. BSD licensed.
*/

// This is Part I of the code
$tbl = "table_name";
$db_uname = 'account';
$db_passwd = 'passwd';
$db = 'database';
$conn = mysql_pconnect('localhost',$db_uname, $db_passwd);

// Path to dbase file
$db_path = "./DBASE.DBF";

// Open dbase file
$dbh = dbase_open($db_path, 0)
or die("Error! Could not open dbase database file '$db_path'.");

// Get column information
$column_info = dbase_get_header_info($dbh);

// Display information
//print_r($column_info);

$line = array();

foreach($column_info as $col)
{
switch($col['type'])
{
case 'character':
$line[]= "`$col[name]` VARCHAR( $col[length] )";
break;
case 'number':
$line[]= "`$col[name]` FLOAT";
break;
case 'boolean':
$line[]= "`$col[name]` BOOL";
break;
case 'date':
$line[]= "`$col[name]` DATE";
break;
case 'memo':
$line[]= "`$col[name]` TEXT";
break;
}
}
$str = implode(",",$line);
$sql = "CREATE TABLE `$tbl` ( $str );";

//set db utf8
mb_internal_encoding('utf8');
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
//set db utf8

mysql_select_db($db,$conn);

mysql_query($sql,$conn);
set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.

// This is part 2 of the code

import_dbf($db, $tbl, $db_path);

function import_dbf($db, $table, $dbf_file)
{
global $conn;
if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
$num_rec = dbase_numrecords($dbf);
$num_fields = dbase_numfields($dbf);
$fields = array();

for ($i=1; $i<=$num_rec; $i++){
$row = @dbase_get_record_with_names($dbf,$i);
$q = "insert into $db.$table values (";
foreach ($row as $key => $val){
if ($key == 'deleted'){ continue; }
$q .= "'" . addslashes(trim(mb_convert_encoding($val,"UTF-8","BIG5"))) . "',"; // Code modified to trim out whitespaces (BIG5 to UTF-8)
}
if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
$q = mb_substr($q, 0, -1);
$q .= ')';
if (!$result = mysql_query($q, $conn)){
print (mysql_error() . " SQL: $q
\n");
print (substr_count($q, ',') + 1) . " Fields total.";
$problem_q = explode(',', $q);
$q1 = "desc $db.$table";
$result1 = mysql_query($q1, $conn);
$columns = array();
$i = 1;
while ($row1 = mysql_fetch_assoc($result1)){
$columns[$i] = $row1['Field'];
$i++;
}
$i = 1;
foreach ($problem_q as $pq){
print "$i column: {$columns[$i]} data: $pq
\n";
$i++;
}
die();
}
}
}

?>

Wednesday, August 01, 2012

Install NagiosQL - note

Reference Official website http://www.nagiosql.org/documentation.html#Installation
yum install gcc libssh2-devel php*
pecl install ssh2-beta

[root@charlie html]# pwd
/var/www/html
[root@charlie html]# ll |grep nagiosql
drwxr-xr-x 11 apache apache   4096 Aug  1 21:01 nagiosql
[root@charlie html]# chown -R apache.apache nagiosql/