Wednesday, July 13, 2011

Convert existing MySQL database from one charset encoding to another

Convert existing MySQL database from one charset encoding to another

Warning: Always make backups.


Convert existing MySQL database from one charset encoding to another

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


Convert existing table from one charset encoding to another

ALTER TABLE db_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

Saturday, July 2, 2011

Monday, June 13, 2011

PHP calculate the difference in time between a past date and a future date

  function nicetime($date)
{
    if(empty(
$date)) {
        return
"No date provided";
    }
   
   
$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   
$lengths         = array("60","60","24","7","4.35","12","10");
   
   
$now             = time();
   
$unix_date         = strtotime($date);
   
      
// check validity of date
   
if(empty($unix_date)) {   
        return
"Bad date";
    }

   
// is it future date or past date
   
if($now > $unix_date) {   
       
$difference     = $now - $unix_date;
       
$tense         = "ago";
       
    } else {
       
$difference     = $unix_date - $now;
       
$tense         = "from now";
    }
   
    for(
$j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       
$difference /= $lengths[$j];
    }
   
   
$difference = round($difference);
   
    if(
$difference != 1) {
       
$periods[$j].= "s";
    }
   
    return
"$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>

Date Function Formatting of PHP

DAYS
d - day of the month 2 digits (01-31)
j - day of the month (1-31)
D - 3 letter day (Mon - Sun)
l - full name of day (Monday - Sunday)
N - 1=Monday, 2=Tuesday, etc (1-7)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6)
z - day of the year (1=365)
WEEK
W - week of the year (1-52)
MONTH
F - Full name of month (January - December)
m - 2 digit month number (01-12)
n - month number (1-12)
M - 3 letter month (Jan - Dec)
t - Days in the month (28-31)
YEAR
L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
Y - four digit year (Ex. 1979, 2006)
y - two digit year (Ex. 79, 06)
TIME
a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)
OTHER
e - timezone (Ex: GMT, CST)
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date

Sunday, June 5, 2011

Set unicode database for sqlserver

Right click database--> Property-->Option-->Collation: Corsican_100_BIN2

Database field: Chose NVARCHAR

by cno

Sunday, May 8, 2011

SQL to Select a random row from a database table

There are lots of ways to select a random record or row from a database table. Here are some example SQL statements that don't require additional application logic, but each database server requires different SQL syntax.

Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Thanks Tim

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Saturday, April 30, 2011

Guide:How to connect CentOS to internet (Bridge connection)

This simple guide is for those who cant still connect their CentOS to internet using a router. I'm using "Bridge" connection.

1. Disable first VMware network adapter (right click then click disable).



2.. Right click your Local Area Connection, click properties then tick or enable VMware bridge protocol. click OK



3.Open your Vmware Workstation then change Ethernet to "Bridge".



Click "start this virtual machine".
Login as " root ".

4. (CentOS) Go to System,Administration,Network.
Click DNS tab then check if you have Primary/Secondary DNS & DNS search path filled.


If those are blank then go to you Windows click Start,Run type cmd click ok then on DOS windows type "ipconfig /all"



Save. then try to browse internet. If doesnt work restart CentOS.

Note: If you want to set Centos IP static, the same as the pattern of you windows IP address click Devices then click etho (make sure it is active) then
click edit, click or tick "Statically set IP addresses" then fill in.



Ex. if your Windows ip is 192.168.0.101 then type 192.168.0.102 or (other IP which is not yet assigned by your router to other computer in your network.)
then just copy your Windows Subnet mask and Default gateway.
Click ok then save and try to browse internet, if doesnt work restart CentOS.