Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:57:55+00:00 2026-06-08T06:57:55+00:00

this is my problem: I have a table in my mysql database which contains

  • 0

this is my problem:

I have a table in my mysql database which contains some products, and it has two columns called “date_from” and “date_to” which determine the period when the product is active.

On my website a have a form where the admin can choose two datetime values and if the product’s active period intersects the span that the admin chose, the page updates a product list.

This is my query, which I am using to achieve what I want:

$sql="SELECT * FROM presents WHERE (date_from>".$date_from." AND date_to<".$date_to.") OR (date_from>".$date_from." AND date_to>".$date_to.") OR (date_from<".$date_from." AND date_to>".$date_to.") OR (date_from<".$date_from." AND date_to<".$date_to.")";

I get this error though:

mysql said: MySql error number: 1064 MySql error decription: You have
an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ‘.26. 12:11:43
AND date_to<2012.07.30. 12:11:46) OR (date_from>2012.07.26. 12:11:’ at
line 1

Any ideas?

*EDIT

I changed the query as suggested and added single quotes to variables. Now I don’t get the error, but the query doesn’t seem to do what it’s supposed to – the product list stayes the same… Here’s the query now:

$sql="SELECT * FROM presents WHERE (date_from>'".$date_from."' AND date_to<'".$date_to."') OR (date_from>'".$date_from."' AND date_to>'".$date_to."') OR (date_from<'".$date_from."' AND date_to>'".$date_to."') OR (date_from<'".$date_from."' AND date_to<'".$date_to."')";

Let’s say I have this row in my table:

ID Name date_from date_to

1 test 2012-07-24 16:40:00 2012-07-31 16:40:00


I need all of these periods to return the row:

2012-07-23 16:40:00 to 2012-08-01 16:40:00

2012-07-23 16:40:00 to 2012-07-28 16:40:00

2012-07-26 16:40:00 to 2012-07-28 16:40:00

2012-07-26 16:40:00 to 2012-08-01 16:40:00

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:57:57+00:00Added an answer on June 8, 2026 at 6:57 am

    You need to quote your variable.

    But it is better not to concat sql, use placeholders instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning php! I have a sql database which contains a table called 'images' which
I have a MySQL table which contains a list of all possible products a
I have a table with a field which contains strings in my MySQL database.
I have a database which contains an Assignment table. The table looks like this:
hello all this seems to be my problem I have a table in mysql
Problem: I have a value in a database table. This value can either contain
So i'm having this problem. I have two tables (Oracle), one is called Destination
So I have this website that has a search feature which searches a table
I have a database package which contains some modules namely student_table and a db
I have 5 tables in MySQL, but i believe this is a SQL problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.