0

I am trying to get data from database if has equal value, this is mySQLi query part of where:

Query:

car_year BETWEEN '$yearArray[0]' AND '$yearToArray[0]'

for example:

car_year BETWEEN '2016' AND '2018'

1st Problem:

It return data from 2016 but until 2017, not 2018.

2nd Problem:

If value be like these:

car_year BETWEEN '2018' AND '2018'

It return nothing, but I want to return all data in this value.

I also tried:

car_year >= '$yearArray[0]' AND car_year <= '$yearToArray[0]'

But if:

car_year >= '2017' AND car_year <= '2017'

It return empty but should return data in this value (2017).

Any idea how can I solve these two problems?


Update:

if ($year && $yearTo) {

  $cleanYearTo = mysqli_real_escape_string($connection, $yearTo);
  $yearToArray = explode(",", $cleanYearTo);

  $cleanYear = mysqli_real_escape_string($connection, $year);
  $yearArray = explode(",", $cleanYear);

  $sqli = "SELECT * FROM car WHERE car_year BETWEEN '$yearArray[0]' AND '$yearToArray[0]'";

// rest of code
}

Sample URL:

mywebsite.com/index.php?y=1397,2018&yt=1397,2018

This return nothing.

7
  • 2
    which data type is car_year? .. and add a proper data sample Commented Apr 7, 2018 at 19:53
  • @scaisEdge VARCHAR, is it matter? Commented Apr 7, 2018 at 19:55
  • @scaisEdge but sorry, what do you mean of proper data sample? Commented Apr 7, 2018 at 19:58
  • 1
    show the data you are using for test .and the query you are using .. Commented Apr 7, 2018 at 20:03
  • @scaisEdge is it enough? Commented Apr 7, 2018 at 20:10

1 Answer 1

1

Your problem obviously clear, people on comments may be confused due you didn't write what data on your database, but they right, your problem is your data type, you data has comma in database like 1397,2018 right? so you are trying to find 2018 in 1397,2018, think twice! it is impossible, unless use LIKE operator like %2018% so at least your query should be like this:

$sqli = "SELECT * FROM car WHERE car_year BETWEEN '$yearArray[0].",".$yearArray[1]' AND '$yearToArray[0].",".$yearToArray[1]'";

In other hand, you need to find year by both value, 1397,2018 not only 2018 unless use LIKE operator. Also you can do this without explode:

$sqli = "SELECT * FROM car WHERE car_year BETWEEN '$cleanYear' AND '$cleanYearTo'";

But recommend to use:

$sqli = "SELECT * FROM car WHERE car_year LIKE '%$cleanYear%' AND car_year LIKE '$cleanYearTo'";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.