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 9096701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:55:11+00:00 2026-06-16T23:55:11+00:00

Disclaimer: still a beginner. I am making a small script to show who is

  • 0

Disclaimer: still a beginner.

I am making a small script to show who is supposed to be on shift at the present, filtered by role. These shifts do go over midnight. As of right now, the time and role is hard-coded, but I intend on allowing the user to specify both later on (hence why I’m not using NOW() and such).

So given the data below, say it’s 4:30 PM on Monday and I want to know what fruits are on schedule, when this script is executed, it will echo out as follows:

Apple
Banana

However, when the code below is executed on the table below, it pretty much always selects the entire table. I am certain there is an issue with the query itself, but I included the rest just for sake of being thorough. I have tested selecting just the day and role itself and it works fine, but something in the way that it selects time evaluates to true erroneously. I’d appreciate any suggestions.

Here is an example of my table:

+----------+-------------+----------+----------+-------------+
| Name     | Days        | Start    | End      | Role        |
+----------+-------------+----------+----------+-------------+
| Apple    | Sun,Mon,Tue | 15:30:00 | 20:00:00 | Fruit       |
| Banana   | Mon,Tue,Wed | 16:00:00 | 20:30:00 | Fruit       |
| Cheese   | Mon,Tue,Wed | 08:30:00 | 10:30:00 | Dairy       |
| Carrot   | Fri,Sat,Sun | 22:00:00 | 07:00:00 | Vegetable   |
+----------+-------------+----------+----------+-------------+

Here is my code, nothing left out:

<?php
    date_default_timezone_set('Etc/GMT+6'); // Set default time zone
    $time = date("H:i:s"); // Time with seconds in a 24-hour, leading zero format: i.e. 07:00:00 or 15:30:00
    $today = date("D"); // Today, in a three-letter format: i.e. Mon, Tue
    $role = "Fruit";

    $username="example-username";  
    $password="example-password";
    $database="example-database";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT *
    FROM work 
    WHERE `days` LIKE '%$today%'  AND
    `role` LIKE '%$role%' AND 
    (start > end AND ('$time' < end OR '$time' > start)) OR
    ('$time' BETWEEN start AND end)";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    $i=0;
    while ($i < $num) {

    $victim=mysql_result($result,$i,"name");

    echo        "<li>$victim</li>";

    $i++;
    }
?>
  • 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-16T23:55:12+00:00Added an answer on June 16, 2026 at 11:55 pm

    Your parentheses are wrong, your query has OR ('$time' BETWEEN start AND end) on the end, so a row that meets this criteria will ignore the role and days clauses. You want an extra pair around

    ((start > end AND ('$time' < end OR '$time' > start)) OR
    ('$time' BETWEEN start AND end))
    

    As a general point, using comma separated values in a database is cutting against the grain of a relational database. Things usually work out better if you have separate rows for each.

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

Sidebar

Related Questions

Disclaimer: i still suck at writing scripts please be gentle if this is a
Again disclaimer disclaimer still learning C# and OOP generally so I hope you'll be
DISCLAIMER : I am still a Rails/Cucumber newbie but I am trying to learn.
DISCLAIMER: I'm still an uber-n00b with programming concepts and know just enough VBS to
As a disclaimer I shall say that I'm still trying to wrap my head
Disclaimer : I'm still starting out with MVC3 (and OAuth in particular). I might
EDIT: Ok I am a bit closer but still need help... http://pastebin.com/9vdhGT2V Disclaimer: This
First of all, a disclaimer: I have experience in other languages, but am still
I added this in manifest theme.dialog <activity android:name=.Disclaimer android:theme=@android:style/Theme.Dialog/> in my activity still I
Disclaimer: I am still learning SQL so I apologize if my question comes off

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.