What is PHP?

June 22, 2008 – 8:25 am

PHP is a server side scripting language, widely used in web programming to build web applications. PHP can be used to develop dynamic content pages. Using PHP we can send request and get response or interact with database servers and manage the page content accordingly.

How PHP works

To run the PHP we need a  web server like apache or IIS.  Whenever a request comes, then web server runs the PHP engine and the output of the executed script is sent back to the user.  Depending on the various input conditions the page content can be changed. PHP can easily connect to MySQL database and generate the content accordingly.

Share/Save/Bookmark

Different methods for passing values from one page to another page

June 21, 2008 – 12:44 pm

We often have to pass values of variables between pages in our web site. These are required in many different types. Some time we gather few values from database and want to retain the value throughout the site for some user as the user moves between pages. There are different methods for passing such values of the variables. It can be passed within a site pages or even outside the site. following are few examples.

Passing values using session between pages
This is one of the secured ways to pass the values between pages. The best example of such a system is when we see our user details inside the member section after we logged in. In the member login system a new session gets created if the given details are correct. These value is stored at server end. Whenever a new page is requested by the same user, the server checks the for existing session value and gives the access to that page. This system is more secure and the member doesn’t get any chance to change the values.

Passing values using cookies between pages
Cookies are stored at the user/ client side and values can be passed between pages. Here the client browser can have the option of reject or accept the cookies by changing the security settings of the browser. Sometime this system can fail to pass values between pages if user or client end settings are changed.

Passing values using URL between pages
We can pass values between pages using the URL. These values are visible to the user and others as they appear in the URL. This is not a secure way to transfer important data like password, etc.

Passing values using web forms
Forms are used mainly to collect data and transfer these data to a processing page. Here the processing page uses one of the above methods to pass values to different pages as per the requirements. Forms can be used to pass values to different sites. we can access the data using $_GET or $_POST methods depending the form method

Share/Save/Bookmark

Disable browser back button using javascript

June 8, 2008 – 11:03 am

Some times after filling the form, user oftenly clicks on back button to go back. We can disable this back button using javascript in the following way

<script language=”JavaScript”>
<!–
history.go(+1);

// –>
</script>
<script>
javascript:window.history.forward(1);
</script>

This script sends the user to the same page where he/she is clicking the back button. In another way its just disables the back button

Share/Save/Bookmark

Display total rows count with LIMIT clause in MySQL

June 8, 2008 – 10:46 am

Assume there are lots of records in result set and want to display only few records. This concept is known as the pagination.

The sql query to achieve above is, as follows,

select * from books where bookcost > 3oo order by bookcost limit 0, 25;

The above query will check the condition and returns only 25 records out of hundreds of records. Now if we want to display the pagination which also tells the how many records are there.

Ex: showing record 1 to 25 of 1500

For this we need to use the count(*) option initially and run the query, then we will get the total results. There is one option which also provides the total number of records.

Use SQL_CALC_FOUND_ROWS and FOUND_ROWS() as a second query to find the total rows.

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name  WHERE id > 50 LIMIT 25;
mysql> SELECT FOUND_ROWS();

Here first query outputs the first 25 rows and also calculates the total number of records using the SQL_CALC_FOUND_ROWS option. Then second query actually outputs the total number of rows.

Share/Save/Bookmark

Repair the corrupted table in MySQL

April 6, 2008 – 4:42 pm

Sometimes, high load on the server, multiple INSERTs and UPDATEs, many SELECT query execution, or hardware failure, your database server may corrupt a table.

The corrupted table can be repaired back using the following statement:

REPAIR TABLE tablename;

Share/Save/Bookmark

Create or copy an existing MySQL table to a new table

April 6, 2008 – 4:28 pm

You can create a new table that looks like another table. That is newly created table will have same structure and definition as of an existing table. The definitions that are copied are: Column names, Data type, precision, length, and scale, Column text.

CREATE TABLE Employee1 LIKE Employee;

Above statement creates the new table Employee1 same as Employee table with the same columns and definition in it.

INSERT Employee1 SELECT * FROM Employee;

This statement copies the data from Employee to new table Employee1. You can also specify the specific database names if you want to copy the database1.table1 in database2 as table1.

Share/Save/Bookmark

Pagination using LIMIT clause in MySQL

April 6, 2008 – 4:14 pm

MySQL supports a really cool feature for pagination is LIMIT clause.

The LIMIT clause is used to limit the number of results or rows returned in a SQL statement. So if you have 500 rows in a table, and only want to return the first 20, you would do something like this:

SELECT column1, column2, column3 FROM tablename LIMIT 20;

LIMIT clause always goes at the end of the query on MySQL. Now suppose if you wanted to show results 21-30.

SELECT * FROM tablename LIMIT 20,10;

This feature can be used in the pagination for multiple records. Pagination can achieved by passing the page number and number of results or rows per page in the coding.

SELECT * FROM tablename LIMIT $page_number*$rows, $rows; #php coding

For Example:
$rows = 10 (per page)
case page = 0: SELECT * FROM tablename LIMIT 0,10; #first 10 records
case page = 1: SELECT * FROM tablename LIMIT 10,10; #first 11-20 records
case page = 2: SELECT * FROM tablename LIMIT 20,10; #first 21-30 records
……
……

Share/Save/Bookmark

How to Add or Drop a column from an existing MySQL table?

January 12, 2008 – 9:05 am

Add a column to MySQL table

To add a new column col_name to the existing table table_name of VARCHAR datatype or any datatype then use ALTER TABLE as follows use the following SQL statement:

ALTER TABLE table_name ADD col_name VARCHAR(size);

This statement by default adds a new column at the end of the table.

ALTER TABLE table_name ADD col_name VARCHAR(size) FIRST;

This statement adds the new column at the begining or starting of the table.

ALTER TABLE table_name ADD col_name VARCHAR(size) AFTER col_name3;

This statement adds the new column after col_name3.

Drop a column from an existing MySQL Table

To drop a column from the existing table use DROP as below

ALTER TABLE table_name DROP col_name;

Share/Save/Bookmark

Cascading Style Sheets, CSS

December 29, 2007 – 6:33 pm

CSS refers Cascading Style Sheets. CSS is used to give the styles to the page elements to look the page more attractive using nice fonts, colors and layouts, format your text, links, backgrounds, tables, input fields. This is very popular on the web and used by web masters to manage the look and feel of the pages of their web site.

For example, for making layout structures, to give font styles, for giving nice borders with different colors, etc.

CSS can be embedded in a page in following ways.

1. <p style=”color:#ff0000;”>CSS embedded in the element directly</p>

2. <style type=”text/css”>
body {font-size:12px; line-height:1.5em;}
.content {color:#666666;}
</style>

3. <link rel=”stylesheet” href=”stylesheet.css” type=”text/css”>
For the first 2 methods we can add the style in the same page. where as for the 3rd method the css file will be outside of the page and we have to refer that css using 3rd option.

Share/Save/Bookmark

Sanchan world blog

November 10, 2007 – 8:41 am

Welcome to Sachan world blog. Find the CSS, php, javascript, ASP, HTML and other programming helps and tips for your web application

Share/Save/Bookmark