Friday, September 3, 2010
Create an array containing a range of elements in PHP
Answer:
It is easy to create an array using the range() function, e.g.
<?php
foreach (range(0, 10) as $number) {
echo $number, " ";
}
It shows following when executed:
0 1 2 3 4 5 6 7 8 9 10
Thursday, September 2, 2010
Simple try catch block in Perl
Answer:
Perl don't have try/catch statement by default, but you can simulate using the following way:
eval {
print 1/0;
};
if ($@) {
print "Error message: $@";
};
When executed, it shows:
Error message: Illegal division by zero at test.pl line 7.
Wednesday, September 1, 2010
Can't connect to local MySQL server through socket
Answer:
Sometimes when you type the mysql command, it shows:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
The cause of this problem is the MySQL socket file is not in the default location, e.g. /tmp/mysql.sock
To fix, edit the MySQL configurations, e.g. /etc/my.cnf, add the following lines
[client]
socket=/tmp/mysql.sock
Add a User in Linux
Answer:
Add a User in Linux, just use the useradd command:
# sudo useradd -d /home/john -m john
The above command will create the user john and create a home directory which is at /home/john
Fix the warning of: Remote Host Identification Has Changed error and solution
Answer:
You might experience this error, especially when you are connecting to a remote Linux machine which has just got re-installed.
To solve this, enter the command:
# ssh-keygen -R 192.168.2.36
Where 192.168.2.36 is the IP you are connecting.