1

Having problems modifying an existing MySQL query and every time I try I keep getting parse errors or MySQL warnings/errors. I have looked at previous questions and tried various things but just can't seem to get it right and it must be something simple.

Here is the working query

mysql_query("
    SELECT count(list_id) AS counter 
    FROM bookings 
    WHERE 
        '". $date_form . ($i - ($startday + 1)) ."' BETWEEN checkin_date 
        AND DATE_SUB(checkout_date, INTERVAL 1 DAY) 
        AND appmt_id = ". mysql_real_escape_string($_POST['appmt_id'])
);

I want to simply add:

AND is_deleted = 0

is_deleted is a field in the table containing either 0 or 1

Have tried with brackets, quotes etc - apologies for the simple question and thanks in advance for any help.

6
  • Can you show the non working query? Commented Oct 22, 2014 at 18:29
  • echo out the query before executing it and see if it looks odd. Also surround the variable you are equating to appmt_id with single ticks. Commented Oct 22, 2014 at 18:31
  • Have tried various things - the code in the original works fine - it's just my addition that doesn't work. Here was my last attempt mysql_query("SELECT count(list_id) as counter FROM bookings WHERE '".$date_form.($i - $startday + 1) ."' between checkin_date and DATE_SUB(checkout_date, INTERVAL 1 DAY) and appmt_id=".mysql_real_escape_string($_POST['appmt_id']) AND (is_deleted == "0")); Commented Oct 22, 2014 at 18:34
  • In mysql equal operator is = not == . Also remove brackets if is_deleted is boolean Commented Oct 22, 2014 at 18:37
  • just tried it again with one = and the error I get is: 'Parse error: syntax error, unexpected '=' ' Commented Oct 22, 2014 at 18:40

1 Answer 1

3

This should work fine if the field is called "is_deleted":

mysql_query("
    SELECT count(list_id) AS counter 
    FROM bookings 
    WHERE '" . $date_form . ($i - ($startday + 1)) . "' BETWEEN checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) 
      AND appmt_id = " . mysql_real_escape_string($_POST['appmt_id']) . "
      AND is_deleted = 0
");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Dan - spot on works perfectly now - will save this for future reference as I have spent too long on this ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.