Jump to content

mysql query problem


chemicaluser

Recommended Posts

Hope someone can help. i am trying to search between two date ranges but am not sure how to add it to the rest of my query.

 

this is my original query which works fine .. it does not have the start and end date part

$query = "SELECT * FROM table WHERE hdt1 like '%$hdt1_search%' and hdt2 like '%$hdt2_search%' and hdt3 like '%$hdt3_search%' and dateadd like '$dateadd_search'";

 

this is the start date to end date search, ...it works ok when it is the only thing in the query

$query = "SELECT * FROM table WHERE dateadd >= '$start_date' AND dateadd <= '$end_date'";

 

 

 

 

BUT, when i try to put it all together the only two fields that get search are the  start date to end date search. ...

 

 $query = "SELECT * FROM table WHERE hdt1 like '%$hdt1_search%' and hdt2 like '%$hdt2_search%' and hdt3 like '%$hdt3_search%' and dateadd like '$dateadd_search' and  dateadd >= '$start_date' AND dateadd <= '$end_date' ";

 

 

what am i doing wrong????

 

 

Link to comment
Share on other sites

Try parentheses around the date range

$query = "SELECT * FROM table WHERE hdt1 like '%$hdt1_search%' and hdt2 like '%$hdt2_search%' and hdt3 like '%$hdt3_search%' and dateadd like '$dateadd_search' and  (dateadd >= '$start_date' AND dateadd <= '$end_date') ";

Link to comment
Share on other sites

There's nothing logically wrong with your complete query.

 

If it didn't return the results you expected, either you didn't save the changed code or your didn't upload the changed file to your web server or the results you got where actually correct for the data you have and what you searched for.

Link to comment
Share on other sites

I tried the parentheses but it is still not working..

 

once i add this part its the only part of the query that returns results ...

dateadd >= '$start_date' AND dateadd <= '$end_date'

 

and once i delete it ... the rest of the query works without problems

 

...i know that i am saving and uploading the right file because when i change the code i get different results

Link to comment
Share on other sites

I tried the parentheses but it is still not working ... sort of

 

once i add this part to the query it becomes mandatory to have dates entered  in the start/end field during the search, otherwise it returns no results

dateadd >= '$start_date' AND dateadd <= '$end_date'

 

but once i delete it ... the rest of the query works without problems

 

...i know that i am saving and uploading the right file because when i change the code i get different results

Link to comment
Share on other sites

Unless you post exactly what you are putting in to the query (echo the $query variable in your code and post what it is), what your data is in your table that you expect the query to match, what result you are getting, what result you expect, and what your actual code is that is executing the query and retrieving and displaying the results, no one here can directly help you.

 

You are expecting us to be able to guess based on "it is still not working" and "the rest of the query works without problems" what your code and data is, what you searched for, and what you saw in front of you that leads you to believe that something that you are doing worked or didn't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.