After upgrading to 10.8.2 I have got a problem with adding a new entry into hosts file: the update was recognised only after system restart. But a more easy solution was just to flash DNS cache! So,
... flushing your DNS cache in Mac OS X is actually really easy, and there are two different commands to use, one for Leopard and for Tiger. Depending on your version of OS X, open your Terminal and follow the appropriate directions below:
Flushing DNS Cache in OS X Lion (10.7) and OS X Mountain Lion (10.8)
Launch Terminal and enter the following command, you will need to enter an administrative password:
sudo killall -HUP mDNSResponder
Note the dscacheutil still exists in 10.7 and 10.8, but the official method to clear out DNS caches is through killing mDNSResponder. You can also find that process running in Activity Monitor.
Flush DNS Cache in Mac OS X 10.5, Mac OS X 10.6
Launch Terminal and issue the following command:
dscacheutil -flushcache
All done, your DNS has been flushed. On a side note, the dscacheutil is interesting in general and worth taking a look at, try the -statistics flag instead for some stats.
Flush your DNS Cache in Mac OS X 10.4 Tiger
Type the following command in the Terminal:
lookupd -flushcache
That’s it - now your DNS settings should be as you intended them to be :-)
So, the task is to get duplicate records from a MySQL database.
The easy way:
SELECT COUNT(*), column1, column2 FROM tablename
GROUP BY column1, column2
HAVING COUNT(*)>1;
More complex case: shows each duplicated row:
It can be done using subquery:
SELECT firstname, lastname, list.address FROM list
INNER JOIN (SELECT address FROM list
GROUP BY address HAVING count(id) > 1) dup ON list.address = dup.address
or with INNER JOIN:
SELECT a.firstname, a.lastname, a.address
FROM list a
INNER JOIN list b ON a.address = b.address
WHERE a.id <> b.id
If the same 'address' exist more than two times, then DISTINCT is needed.
When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just the solution for you. Here’s a way to find and replace a string of text in one or more files automatically.
For the purpose of this exercise we will use a Linux command line tool called “sed”. ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. We are using a very limited aspect of “sed” here. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting.
We are going to use the following syntax to find and replace a string of text in a file:
# sed -i 's/[orginal_text]/[new_text]/' filename.txt
Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. You have just switched to a new database server and need to update it with the new server’s IP address. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Here’s how you go about it:
Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Here’s the breakup of the above command. First you call the “sed” command. Then you pass it the parameter “-s” which stands for “in place of”. Now we use a little bit of regular expressions, commonly known as “regex” for the next bit. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. Between them they result in a “global substitution of the the string of text you place in between them.
You can optionally skip the “g” at the end. This means that the substitution will not be global, which practically translates to the substitution of only the first instance of the string in a line. So if you had a line with multiple instances of the text you are trying to replace, here’s what will happen
Here comes the real magic. Now, say you want to change a string of text not just in a single file, but in the entire directory you are in. There are a number of text files in which you need to find and replace the “wine” with “champagne”.
# find . -maxdepth 1 -name "*.txt" -type f -exec sed -i 's/wine/champagne/' {} \
We use the find command to get a list of all the text files in the current directory. That’s the “find . -maxdepth 1 -name “*.txt” -type f” part. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. The ‘-name ”*.txt”‘ part tells find to only list files with the extension of “.txt”. Then the “-type f” section specifies that “find” should only pick exactly matching files. Finally the “-exec” part tells “find” to execute the command that follows, which, in this case, is the “sed” command to replace the text – “sed -i ‘s/wine/champagne/’ {} \”.
I realize that the above command seems complicated. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Now try changing a string of text in multiple levels of directories.
In case of date format checking in PHP just use this function:
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
}
In case of MySQL error 2006 (HY000) at line: ### MySQL server has gone away which may occur while restoring a big DB dump just add (or increase, if exists) max_allowed_packet parameter of MySQL config (usually in /etc/my.cnf).