Friday, September 18, 2009

Executing PHP code from a .html file

You can execute PHP on a .html page by adding following line in your .htaccess file :

AddType application/x-httpd-php .html

for executing PHP on a .htm page add this line instead :

AddType application/x-httpd-php .htm

The above code will make php executable on all your .html or .htm pages

If you want to make PHP executable only on one page, you may add following line in your .htaccess file:

<Files myhtmlpage.html> AddType application/x-httpd-php .html
</Files>


The above code would make PHP executable only on myhtmlpage.html and not on all html pages.

Creative Commons License

Wednesday, April 22, 2009

Workaround for Maximum Script Execution time limit

We all know that the default execution time of a php script is set to 30 seconds, defined by the parameter max_execution_time in php.ini file.

A simple workaround to overcome the time limitation is a call to ini_set() as :

ini_set('max_execution_time','0')   //0 = no limit


This causes the current script to be executed without any maximum time limit. I found this workaround helpful in case of sending newsletters where bulk mailing is required. Hope it is helpful to you.

Creative Commons License

Tuesday, April 21, 2009

Singleton Design Pattern Example in PHP

Here is a sample example class that implements Singleton Design Pattern in PHP:

require_once("properties.php");
class DbConnection
{
private $connectionId;

private function __construct($dbHost,$dbUser,$dbPassword,$dbName)
{
$this->connectionId =
mysql_connect($dbHost,$dbUser,$dbPassword,true);
if($this->connectionId)
{
$this->selectDatabase($dbName);
}
else
{
throw new Exception(mysql_error());
}
}

private function selectDatabase($db)
{
$dbSelected = mysql_select_db($db,$this->connectionId);
if(!$dbSelected)
{
throw new Exception(mysql_error());
}
}

function __destruct()
{
if(is_resource($this->connectionId))
{
$this->closeConnection();
}
}

public function closeConnection()
{
mysql_close($this->connectionId);
}

public function getResourceId()
{
return $this->connectionId;
}

public static function getInstance()
{
static $dbConnObj;
if(!isset($dbConnObj))
{
$dbConnObj = new DbConnection(HOSTNAME,DB_USER,DB_PWD,DB_NAME);
}
return $dbConnObj;
}

public function prepareQuery($args=null)
{
$resourceId = $this->getResourceId();
$args = func_get_args();
$query = array_shift($args);
foreach($args as $argIndex=>$param)
{
$args[$argIndex] = mysql_real_escape_string($param,$resourceId);
}
return vsprintf($query,$args);
}

public function fetchRow($query)
{
$recordDetails = $this->executeQuery($query);
return $recordDetails[0]? get_object_vars($recordDetails[0]) : null;
}

public function fetchVar($query)
{
$recordDetails = $this->fetchRow($query);
if(count($recordDetails)>0)
{
$values = array_values($recordDetails);
}

if(isset($values[0]) && $values[0]!=='')
{
return $values[0];
}
}

public function executeQuery($query)
{
$resourceId = $this->getResourceId();
$result = @mysql_query($query,$resourceId);
if( preg_match("/^\\s*(insert|update|delete|replace|alter)/i",$query) ){
$returnVal = mysql_affected_rows($resourceId);
}
else{
$resultSet = array();
while($row = @mysql_fetch_object($result))
{
$resultSet[] = $row;
}
$returnVal = $resultSet;
}
return $returnVal;
}
}

try{
$dbConn = DbConnection::getInstance();
}catch(Exception $e){
die("
Error:".$e->getMessage());
}

Note:
1. HOSTNAME
2. DB_USER
3. DB_PWD
4. DB_NAME are constants defined in
properties.php



Creative Commons License

Tuesday, February 3, 2009

File uploading issue

Recently, I was writing a php script for uploading files.Strangely enough, I was able to upload all common types of files i.e.*.mpeg,*.avi,*.swf files but i was unable to upload flash files.

As i was aware that the default file upload size,controlled by upload_max_filesize php.ini directive is 2MB and the default maximum size of post Data that PHP accepts, controlled by post_max_size php.ini directive is 8MB, i tried changing value of upload_max_filesize using ini_set() function to 8MB. But there was no effect of this as i found that ini_set() doesn't necessarily change all the directives.

Then as a test, i made following changes directly in php.ini file:
1. set upload_max_filesize to 8MB
2. set post_max_size to 9MB
And voila, it worked. I was able to upload flash files(.flv) as well alongwith other video file types.

Note: Files are usually POSTed to the webserver using 'multipart/form-data' encoding format.What post_max_size does is,to set the upper limit on the amount of data that a script can accept.

Hence,post_max_size is the upload_max_filesize plus the sum of the lengths of all the other fields in the form.

Finally, after checking php manual and php.net link, i figured out following rules to follow before using ini_set() function

1. Make sure that the "CHANGEABLE" attribute of the php.ini directive whose value you would like to alter, is either PHP_INI_USER OR PHP_INI_ALL. Only those directives with these "CHANGEABLE" values can be modified with ini_set() function.

2. In the above case, make changes in php.ini as mentioned above.

Creative Commons License

Wednesday, January 28, 2009

Tuesday, January 27, 2009

MYSQL - query to find top 10 favourite locations in each city

Here's a short example of using MYSQL CASE WHEN Control flow function alongwith User defined variables


SELECT CITY,Location,Most_Popular
FROM
(
SELECT @CITY,
CASE
WHEN (@CITY=CITYNAME FIELD AND @ROWNUM < 10) THEN (@ROWNUM:=@ROWNUM+1)
WHEN (@CITY != CITYNAME FIELD) THEN @ROWNUM:=1
ELSE @ROWNUM:=10
END AS CHANGED_CITY, @CITY:=CITYNAME FIELD, D.*
FROM (SELECT @ROWNUM:=0) R, (SELECT @CITY:='0') M,
(
SELECT CITY, Location, count(UserId Field) as Most_Popular
FROM Table Names
WHERE
Field Constraints
group by CITY,Location
order by CITY,Most_Popular Desc
)D
)D1
GROUP BY CITY,CHANGED_CITY
order by CITY,Most_Popular Desc;

Thanks to Ankit for helping me out on this...

Creative Commons License

Friday, January 23, 2009

MYSQL CASE WHEN EXAMPLE

Here's a short example of using CASE WHEN control flow statement for grouping users who write reviews by age group:

select CASE
WHEN ( FLOOR(datediff(CURRENT_DATE,DOB)/365) > 15 AND FLOOR(datediff(CURRENT_DATE,DOB)/365) < 25) THEN '15-25'
WHEN ( FLOOR(datediff(CURRENT_DATE,DOB)/365) > 25 AND FLOOR(datediff(CURRENT_DATE,DOB)/365) < 35) THEN '25-35'
WHEN ( FLOOR(datediff(CURRENT_DATE,DOB)/365) > 35 AND FLOOR(datediff(CURRENT_DATE,DOB)/365) < 50) THEN '35-50'
ELSE 'Above 50'
END as Age_Group,
count(*) as Reviews
FROM
Table names
WHERE
Field Constraints
group by Age_Group;

NOTE:
DOB:Date of Birth field



Creative Commons License

Dear Readers

Finally a blog to pen down my daily coding work...!! I have been thinking of setting up a blog for quite a long time where in i could write just about anything new that i keep learning about LAMP Stack and of course!! to keep a track of my coding tricks.....Keep watching this space for more posts...


Creative Commons License