Monthly Archives: March 2012

How to make PHP and MySQL support Arabic, Urdu and charactersets or other foreign languages

To enable your PHP/MySQL script support Arabic or Urdu or other foreign languages, you need to do the following.

In database connection, do the following.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
//Below is an additional line to enable supportĀ for foreign languages.
mysql_query(“SET NAMES utf8″) ;
mysql_select_db($db_name,$cn) or die(mysql_error());

In database, set collation of fields to utf8_general_ci. If you can set collation of databas and tables to utg8_general_ci as well, that will even be better. A sample database script is given below.


– Database: `mytest`

CREATE DATABASE `mytest` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mytest`;

– ——————————————————–


– Table structure for table `mytable`

CREATE TABLE IF NOT EXISTS `mytable` (
`my_id` int(11) NOT NULL auto_increment,
`my_title` varchar(100) NOT NULL,
PRIMARY KEY (`my_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Now you need to set the charset of the page. Add the following meta tag to the head of you web page. If a charset meta tag already exists, then change it to as below.

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>

That’s it!

How to print all the months and years between two dates in PHP

function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date(‘mY’, $time2);

$months = array(date(‘F Y’, $time1));

while($time1 < $time2) {
$time1 = strtotime(date(‘Y-m-d’, $time1).’ +1 month’);
if(date(‘mY’, $time1) != $my && ($time1 < $time2))
$months[] = date(‘F Y’, $time1);
}

$months[] = date(‘F Y’, $time2);
return $months;
}
echo “<pre>”;
print_r(get_months(’2011-02-1′, ’2012-12-1′));

How to set a cron job in CPanel to call a PHP or Web Page

To set a cron job in Cpanel to call a PHP or web page at any given times, do the following.

Go to Cpanel of your web site.

Log in to it.

Go to Cron Jobs. It is under the section called “Advanced”.

Cron Section

Click on it.

This will take you to a new screen as below.

Set Cron Job

One this screen, select the frequency of your choice from “Common Settings”.

Set Cron Job

In the “Command” text box, type the following syntax where http://www.your-site-name.com is the name of your web site.

curl -s http://www.your-web-site.com/path-to-your-file.php>/dev/null 2>&1

*Note:
Sometimes you configure your web site using .htaccess file to open it with or without “www” when someone types the URL. Make sure to follow the same rule here.