Friday, October 10, 2014

MySQL Sintax To Find Unmatched Records Between Two Table


Find difference record between two tables. Here the code:
SELECT username
FROM (
SELECT username FROM member_option
UNION ALL
SELECT username FROM member WHERE status='1'
) tbl
GROUP BY username
HAVING count(*) = 1
ORDER BY username;
Good Luck !!!

MySQL Sintax to Find Duplicate Entries


Here the code:
SELECT column FROM table_name group by column having count(*) >= 2
Good Luck !!!

Thursday, August 21, 2014

Adding foreign key in MySQL


This sql sintax can create link betwen key in some tables. Please used in InnoDB only. Here the code:

ALTER TABLE child_table_name
ADD FOREIGN KEY (P_ID)
REFERENCES parent_table_name (P_ID)

#example:
ALTER TABLE member_detail
ADD FOREIGN KEY (username)
REFERENCES member (username)

#foreign key will be deleted/updated if primary key deleted/updated
ON DELETE CASCADE ON UPDATE CASCADE

#parent will not be deleted if any child row exist
ON DELETE RESTRICT

#The script will be:
ALTER TABLE member_detail
ADD FOREIGN KEY (username)
REFERENCES member (username)
ON DELETE CASCADE ON UPDATE CASCADE

#Delete exist foreign key
ALTER TABLE member_detail DROP FOREIGN KEY member_detail_ibfk_1;

Good Luck !!!

Wednesday, March 26, 2014

Command Linux To Chmod File Only Or Directory Only Recursively In All Subdirectory

This command can chmod file only or directory only recursively. Here the code:
sudo su

#this command to chmod file only recursively
find . -type f -exec chmod 644 {} \;

#this command to chmod directory only recursively
find . -type d -exec chmod 755 {} \;
Good Luck !!!

Monday, March 24, 2014

PHP Code To Convert DateTime To Your Prefer Timezone

This function can convert DateTime to your prefer timezone. Here the code:
function convertTimezone($date,$zone){
$dates   = explode(" ", $date);
$tgl     = explode("-",$dates[0]);
$time    = explode(":",$dates[1]);

$newtime = mktime($time[0]+$zone,$time[1],$time[2],$tgl[1],$tgl[2],$tgl[0]); // H,i,s,m,d,Y
$newdate = date('Y-m-d H:i:s',$newtime);

return $newdate;
}

$date = "2014-03-30 06:01:09";
$newDate = convertTimezone($date,"+7");
echo $newDate;

Good Luck !!!

PHP Code To Make A Random Password

This function can generate a random password, use alphanumeric character, and eliminating the ambiguous character. Here the code:
function randomPassword(){
$digit = 8;
$karakter = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";

srand((double)microtime()*1000000);
$i = 0;
$pass = "";
while ($i <= $digit-1)
{
$num = rand() % 32;
$tmp = substr($karakter,$num,1);
$pass = $pass.$tmp;
$i++;
}
return $pass;
}

$password = randomPassword()
echo $password;
Good Luck !!!

PHP Code To Limiting Words Number In PHP

This function can limiting words number. Usually this used to limit name of person. Here the code:
function word($word,$limit){
$result='';
$words=@explode(" ",$word);
for($i=0;$i<=$limit-1;$i++){
$result.=@$words[$i]." ";
}
return $result;
}

$name = "iben arief bin padi";

echo word($name,1); // output: iben
echo word($name,2); // output: iben arief
echo word($name,3); // output: iben arief bin
echo word($name,4); // output: iben arief bin padi
Good Luck !!!