Shantanu's Blog

Database Consultant

May 20, 2009

 

PHP and shell script

You can call the shell script from PHP page and also check if it was run successfully.

?php
passthru('xfer_rsync.sh',$returnvalue);

if ($returnvalue != 0){
//we have a problem!
//add error code here
}else{
//we are okay
//redirect to some other page
}
?


Read More

Labels: ,


May 13, 2009

 

Cute Curl

Curl is a command line tool for doing all sorts of interesting and essential URL manipulations and data transfers.

// Read a plain URL
curl http://www.google.com

// Get a web page and store it in a file
curl -o savedpage.html http://www.example.com/index.php

// download all the pages from page no 1 to page no 12 and save it to a corresponding file
curl -o pages#1.html http://example.com/pages.php?pageNo=[1-12]

// Show headers only
curl --head http://www.google.com/logo_plain.jpg

// Get a FTP directory listing
curl ftp://username:password@example.com/directory/

// Upload a file to a remote directory using FTP
curl -T uploadfilename -u username:password ftp://sitename.com/directory/myfile

Labels:


May 10, 2009

 

Normalize Table

Normalize table by creating a child table with the uniquefield and each value of the comma separated column.

Name skills
shan PHP, MySQL, HTML, JAVA
Paul MySQL, Perl
Ahmed JavaScript, C++

You can use comma or space as delimiter to split the values as shown below.

CALL normalize_table('Original_table', 'Name', 'skills', ',');

A new table will be created called SplitValues that will look like this...

cid value
Shan PHP
Shan MySQL
Shan HTML
Shan JAVA
Paul MySQL
Paul Perl
Ahmed JavaScript
Ahmed C++

alter ignore table SplitValues add primary key(cid, value), add key(value, cid);

You will like to add keys on this SplitValues table so that you can join it back to the original table. As the column "Name" is unique in the original table, the following query should work.

select a.* from SplitValues as a inner join original_table as b on a.cid = b.Name where a.cid = "Paul"
will return 2 rows instead of 1

cid value
Paul MySQL
Paul Perl

This has various uses, as it can further be joined with some other table. The join will be very fast as both the columns in these two tables are indexed.

http://forge.mysql.com/tools/tool.php?id=204

Labels:


 

Using collation rules in your Application

Suppose we have a web application, users post their names and phone numbers. Phone numbers can have very different format:

mysql> select * from phonebook order by phone;
+-------+--------------------+
| name | phone |
+-------+--------------------+
| Sanja | +380 (912) 8008005 |
| Bar | +7-912-800-80-01 |
| Svoj | +7 912 800 80 02 |
| Ramil | (7912) 800 80 03 |
| Hf | +7 (912) 800 80 04 |
+-------+--------------------+
5 rows in set (0.00 sec)

Searching for a phone number is difficult. For e.g. the following query does not return "Svoj".
select * from t1 where phone='+7(912)800-80-02';

mysql> create table phonebook (name varchar(64), phone varchar(64) character set utf8 collate utf8_phone_ci);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into phonebook values ('Svoj','+7 912 800 80 02');
Query OK, 1 row affected (0.05 sec)
mysql> insert into phonebook values ('Hf','+7 (912) 800 80 04');
Query OK, 1 row affected (0.00 sec)
mysql> insert into phonebook values ('Bar','+7-912-800-80-01');
Query OK, 1 row affected (0.00 sec)
mysql> insert into phonebook values ('Ramil','(7912) 800 80 03');
Query OK, 1 row affected (0.01 sec)
mysql> insert into phonebook values ('Sanja','+380 (912) 8008005');
Query OK, 1 row affected (0.02 sec)

_____

Solution: reorder punctuation characters, making them ignorable

Solution: reorder punctuation characters, making them ignorable

/usr/share/mysql/charsets/Index.xml

Edit the above file and add the code mentioned below.




....



\u0000
\u0020
\u0028
\u0029
\u002B
\u002D






_____

mysql> show collation like '%phone%';
+---------------+---------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+---------------+---------+-----+---------+----------+---------+
| utf8_phone_ci | ucs2 | 252 | | | 8 |
+---------------+---------+-----+---------+----------+---------+
1 row in set (0.00 sec)

mysql> select * from phonebook where phone='+7(912)800-80-02';
+------+------------------+
| name | phone |
+------+------------------+
| Svoj | +7 912 800 80 02 |
+------+------------------+
1 row in set (0.00 sec)


http://forge.mysql.com/w/images/b/b7/HowToAddACollation.pdf

Labels:


May 07, 2009

 

User friendly?

For years, (yes, years!) I did not know how to find the list of all the bugs those I have submitted and to watch the bugs those I have subscribed to. Why? The small check box "only your bugs" and "include subscribed bugs" is hidden deep down. I wish it was at the top somewhere and a mention of the e-mail address that I used to log-in.

Labels:


May 02, 2009

 

Stemming Words in MySQL

Stemming words using mysql user defined function looks very promising.
For e.g. the user might search for "testing", "tested" or "tests". All these words are related to each other because the base word "test" is common in all of them. the stem_word function will find out what the base word is for each word saved in the database and match it against the base word of the user input.

mysql> select stem_word('en', 'testing');
+----------------------------+
| stem_word('en', 'testing') |
+----------------------------+
| test |
+----------------------------+
1 row in set (0.00 sec)

mysql> select stem_word('en', 'tested');
+---------------------------+
| stem_word('en', 'tested') |
+---------------------------+
| test |
+---------------------------+
1 row in set (0.00 sec)

mysql> select stem_word('en', 'tests');
+--------------------------+
| stem_word('en', 'tests') |
+--------------------------+
| test |
+--------------------------+
1 row in set (0.00 sec)

The stem_word function is not there by default and needs to be installed from:
http://mysqludf.com/lib_mysqludf_stem/index.php

This is different than the soundex logic. As you can see from the following screen shot, all the words have different soundex value and hence are considered different. These three words doesn't has any relation to each other if we use the soundex logic, but using the stem_word logic (as explained above) we can easily find the word families.

mysql> select soundex('testing');
+--------------------+
| soundex('testing') |
+--------------------+
| T2352 |
+--------------------+
1 row in set (0.02 sec)

mysql> select soundex('tested');
+-------------------+
| soundex('tested') |
+-------------------+
| T230 |
+-------------------+
1 row in set (0.00 sec)

mysql> select soundex('tests');
+------------------+
| soundex('tests') |
+------------------+
| T232 |
+------------------+
1 row in set (0.00 sec)

Labels:


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023  

This page is powered by Blogger. Isn't yours?