Thursday, October 25, 2007

Delete Vs Truncate Statement

Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow.

• Truncate table also deletes all the rows in a table, but it won’t log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back.

• Truncate table is functionally identical to delete statement with no “where clause” both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete.

• Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc., remains as it is.

• In truncate table the counter used by an identity column for new rows is reset to the seed for the column.

• If you want to retain the identity counter, use delete statement instead.

• If you want to remove table definition and its data, use the drop table statement.

• You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger.

• Truncate table may not be used on tables participating in an indexed view.

Tuesday, October 16, 2007

List some SQL optimisation techniques.

- Do not use SELECT * unless you must
- Do not create indexes you are not going to use
- Indexes are good for reads, but bad for writes
- Use OPTIMIZE TABLE and ANALYZE TABLE regularly
- Create your tables with a fixed-table format if possible
- Use the most efficient table type for each table
- Use the best data type, including NOT NULL if appropriate
- Use default values for INSERT when you can
- Use temporary tables rather than heavy PHP work
- Use SELECT priorities for better control
- SELECT foo IN (list, of, constants) is very fast
- When joining tables, use numbers instead of strings if you can

Session vs Cookies?



 Browser needs Cookies Enabled? Can User Edit Information?

Information Lasts Between Browser Sessions?

(Leaving site and coming back) 

Information Location 
Cookies
 Yes Yes, easily
 Yes User's Browser
Sessions
 No
 No*  No Server, except for session ID
*Users can not modify the information contained in sessions but others can steal session ids and impersonate victims.

Will Session work in PHP if cookies are disabled?

The good news is that sessions will work even if cookies are not enabled. When a session is created, PHP assigns the browser a session ID which, normally, it first tries to store in a cookie. If that fails it will automatically pass the session ID through the url. The nice thing is that PHP takes care of all this for you.

Thursday, October 11, 2007

Can I turn autocomplete ON/OFF programmatically?

As a web developer you *cannot* turn this feature ON programmatically if the user has turned it OFF. That is not allowed.

However, if needed, a web developer can turn this feature OFF either for a particular field or for the entire form like below.

<input name="name" autocomplete="OFF" type="text">

<form autocomplete="OFF">

Wednesday, August 08, 2007

PHP supports an alternative syntax for the various control structures.

Way 1

if ($check == 0) {
echo 'True!';
} else {
echo 'False!';
}
?>

Way 2

if ($check == 0):
echo 'True!';
else:
echo 'False!';
endif;
?>

Sakila

The official MySQL logo is a dolphin named Sakila.

Friday, August 03, 2007

ERROR 2006 (HY000) at line 1: MySQL server has gone away

When i tried to import data to MySQL through large SQL file (4.71MB), i got the above error. To overcome this issue we need to add the below option in my.ini file of MySQL.

max_allowed_packet=16M

Restart MySQL service.

Tuesday, March 27, 2007

How do I prevent Web browsers caching a page using PHP?

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
?>

We can go one step further, using the Cache-Control header that’s supported by HTTP 1.1 capable browsers:

<?php
header("Expires: Mon, 26 Jul 2007 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);
header("Pragma: no-cache");
?>

How do I prevent Web browsers caching a page using HTML?

The most basic approach to doing this utilizes HTML meta tags:

<meta http-equiv="Expires" content="Mon, 26 Jul 2007 05:00:00 GMT"/>

<meta http-equiv="Pragma" content="no-cache" />

By inserting a past date into the Expires meta tag, we can tell the browser that the cached copy of the page is always out of date. This means the browser should never cache the page.

Monday, March 12, 2007

How to force user to download PDF file?

<?php
// Change the header to allow PDF download
header('Content-type: application/pdf');

// The downloaded file will be called as savethis.pdf
header('Content-Disposition: attachment; filename="savethis.pdf"');

// The source PDF to be downloaded is source.pdf
readfile('source.pdf');
?>

Monday, February 05, 2007

Tuesday, January 23, 2007

How to get contents of a remote URL using PHP?

/************** Method 1 ******************/
$request = 'www.remoteurl.com';
$response = file_get_contents($request);
/************** Method 1 ******************/

/************** Method 2 ******************/
$filename = "www.remoteurl.com";
$handle = fopen($filename, "rb");

$response="";

if ($handle) {
while (!feof($handle)) {
$response .= fgets($handle);
}
fclose($handle);
}
/************** Method 2 ******************/

Read this article to know the PHP code performance...

http://www.phplibrairies.com/tutorial_2_page_1_en.html

Monday, January 08, 2007

How to make the available options of select box as selected while submitting a form using javascript?

for(var i = 0;i < document.formname.selectboxname.length;i++) {
document.formname.selectboxname.options[i].selected = true;
}

How to check whether atleast one checkbox is checked out of multiple checkboxes in a form?

Example:

var selected=false;

for(var i=0; i<frmname.Days.length; i++)
{
if(frmname.Days[i].checked==true)
{
selected=true;
}
}

if(selected==false) {
alert("Please select atleast one");
return false;
}

How to get confirmation before submitting a form using javascript?

var agree=confirm("Are you sure you wish to continue?");

if (agree) {
return true;
} else {
return false ;
}