Actually I want user to select two years and then I want to fetch those rows from the mysql database which come in between these selected years.
For example user select year_1= 2006 and year_2= 2009 Then I want to display all the records between year 2006 and 2009
I guess Code will make it easier to understand

Q: It looks like you have two separate columns (year_1 and year_2) on the persons table, and you are intending to check both. Is that correct? Q: If row has year_1=2006 and year_2=2012, should that record be returned or not? If row has year_1=2004 and year_2=2012, should that row be returned? (The query needed to satisfy the specification really depends on what is specified. The query in the (very) good answer from Zane Bien satisfies one interpretation of the spec.
–
spencer7593Jul 14 '12 at 4:06

I guess you got the point. Let me check this query. Isn't it selecting the all values of year_1 which comes between $year_1 and $year_2 ?
–
Absar AkramJul 14 '12 at 3:50

The code you provided first worked perfectly. I have one column actually year_1 and no year_2 so the conclusion goes here $sql="SELECT * FROM names WHERE year_1 BETWEEN $year_1 and $year_2"
–
Absar AkramJul 14 '12 at 5:05

The simplest explanation is that you have only a single "year" column in your table. (We may be mistakenly implying (from the references to "year_1" and "year_2" in your question) that your table has two separate "year" columns you are wanting to check.)

If you have only one "year" column that you are checking, then it's pretty simple query:

But your question seems to reference two separate columns: year_1 and year_2, in addition to your two variables $year_1 and $year_2.

So, another possible interpretation of the spec is that the year_1 and year_2 columns are lower and upper bounds of a continuous range, and you want to find rows that satisfy an entire given range of years.

As an example, year_1 might be "birth year" and year_2 might be "death year", and I am wanting to find all rows from the persons table who were "alive" EVERY year in a given range: