Conditions in PHP

  1. You should quote $depx

if($giveortake=='3' || $giveortake=='5') { $sql .=' AND departureDate <= "'. $depx .'" + INTERVAL '.$giveortake.' DAY'; }

(my bad, sorry)
in the code you have now you’re asking “departureDate < 2010-07-05”, which MySQL interprets to mean “is departureDate smaller than, or equal to, 1998”, because 2010-7-5 is 1998.

  1. If it all possible, don’t use MySQL functions in your WHERE clause, because MySQL has to apply that function to each and every record before it can begin to answer the query, i.e., using a function in the WHERE clause forces a full table scan, which is a bad thing.
    So, you should change

where DATE_FORMAT(departureDate, '&#37;d/%m/%Y') = '$dep' 

to


where departureDate = '$depx' 

(and of course make sure $depx is defined at that point).

  1. The query you’re creating doesn’t make any sense.
    If I were to input “07/05/2010” as date, and “5” as giveortake, you would have the following in your WHERE clause:

departureDate = '2010-05-07' AND departureDate <= '2010-05-07' + INTERVAL 5 DAY

Since you’re already asking every record where date=‘2010-05-07’, adding departureDate <= ‘2010-05-07’ + INTERVAL 5 DAYS doesn’t add anything.
I think you should look at BETWEEN.
That is, if a user enters a giverotake, don’t use the date=<user-input>, but use a BETWEEN statement instead.
Oh, and you’ll also need CURDATE().

Ok I see your first point. I did change it in the primary query to state; where departureDate = ‘$depx’

in the conditionusing the code below, it is returning results but only that matches the primary query.

I am not sure how to use the between option as not sure how to implement in the conditions bit and syntax etc, so just trying to get this working.

if($giveortake==‘3’ || $giveortake==‘5’) { $sql .=’ AND departureDate <=‘. "’$depx’" .’ + INTERVAL ‘.$giveortake.’ DAY’;}

what do you think? could it be the syntax or format?

Thanks

Please re-read my previous post:

if a user enters a giverotake, then don’t use the date=<user-input>, but (else) use a BETWEEN statement.

ok but I am not sure how to use between in this scenario. Would it be something like this?

if($giveortake==‘3’) { $sql .=’ AND departureDate between “‘$depx’” and “‘$depx’” + 3;}

You still need the INTERVAL 3 DAY, and it needs to be INSIDE the string …

‘some text in a string’ + 3 = 3

I am not sure if i understand you correctly. do you mean something like this?

if($giveortake==‘3’) { $sql .=’ AND departureDate between “‘$depx’” and “‘$depx’” .’ + INTERVAL + 3 = 3;}

No, I don’t.
Please read my previous post again.
Also please read post #23 again, since I’ve got this feeling you still don’t know what I said there.

I think you really should get a decent book on how to write PHP and MySQL, because it looks like your just hacking random characters together and hope it works. Programming doesn’t work that way. No offence.

I have a PHP book and I looked at onlines tutorials, but cant find anything that explains like this scenario. We managed to sort out the duration bit which I understand, but the bit with INTERVAL is not straightforward.

You mention to use the Between bit but the examples I found in the book are simple and just compare between two dates… In my case, I have to use the Interval as well so Perhaps some clarification with details on your previous comments would help!

Okay, let me try to break this down for you


if($giveortake=='3') { $sql .=' AND departureDate between "'$depx'" and "'$depx'" + 3;}

The problem here is that ’ AND departureDate between “‘$depx’” and “‘$depx’” is a string, and 3 is an integer and you’re using + to try to concatenate them. However, + is not the operator to concatenate string, but . (dot) is.
Effectively what you’re doing here is add ’ AND departureDate between “‘$depx’” and “‘$depx’” and 3 together, and since ’ AND departureDate between “‘$depx’” and “‘$depx’” is not a number it will evaluate to 0, thus you have 0 + 3 = 3.

Also, you’re ending the string and opening it again wrongly.
Key to understand here is that there are strings with single quotes and string with double quotes. These have different meaning. Strings in double quotes are parsed by PHP, while string in single quotes are not.

To give you an example


$name = 'ScallioXTX';
echo 'Hi, my name is $name'; // Hi, my name is $name
echo "Hi, my name is $name"; // Hi, my name is ScallioXTX

Note the difference between the single quotes and the double quotes.

So to combine this, you should have done this:


if($giveortake=='3') {
  $sql .= " AND departureDate BETWEEN '$depx' AND '$depx' + INTERVAL 3 DAY";
}

That is, the value of the column departureDate should lie between $depx and $depx + 3 days

Now, for the if-then-else part (as per post #23).

If you select all rows that have a specific departureDate and also have a departureDate between that same date and another date, you end up with all rows that have the specific departureDate, and the interval is ignored.
This is easier to understand if we swap the different clauses in the WHERE query. Suppose we have this query

[SQL]
SELECT
field1,field2,field3
FROM
some_table
WHERE
departureDate BETWEEN ‘2010-05-10’ AND ‘2010-05-13’
AND
departureDate = ‘2010-05-10’
[/SQL]

Now, MySQL evaluates this query from left to right and will first find all rows that are between ‘2010-05-10’ and ‘2010-05-13’, so you might have an intermidiate result that contains the dates ‘2010-05-10’, ‘2010-05-11’ and ‘2010-05-13’. Now, we go over to the next part, WHERE deparureDate = ‘2010-05-10’. Clearly, ‘2010-05-11’ and ‘2010-05-13’ are not equal to ‘2010-05-10’ so they get dropped from the result, leaving you with only ‘2010-05-11’.

So, what you need to do is change the check for the departureDate in the WHERE clause.

So, your complete code thusfar would be


$dep = mysql_real_escape_string($dep);
$dest = mysql_real_escape_string($dest);

// rewrite $dep to YYYY-mm-dd format, and call that $depx
list($day, $month, $year) = split( '[/]', $dep);
$depx = "$year-$month-$day";

// Change MySQL keywords to uppercase, so you can discern them better from the rest of the code

$sql = 'SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, "%d/%m/%Y") AS formattedDate FROM holidays WHERE ';

if ($giveortake == '3' || $giveortake == '5') {
  // We now need to search in a range of dates, using BETWEEN and INTERVAL
  $sql .= 'departureDate BETWEEN "'.$depx.'" AND "'.$depx.'" + INTERVAL '.$giveortake.' DAY';
} else {
  // We need to check for exactly one date
  $sql .= 'departureDate = "'.$depx.'"';
}

if($duration=='7' || $duration=='3') {
  $sql .= ' AND duration='.$duration;
}

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
  outputProduct($row);
}

Hm, thats not entirely correct I see.
It should be:


$dep = mysql_real_escape_string($dep);
$dest = mysql_real_escape_string($dest);

// rewrite $dep to YYYY-mm-dd format, and call that $depx
list($day, $month, $year) = split( '[/]', $dep);
$depx = "$year-$month-$day";

// Change MySQL keywords to uppercase, so you can discern them better from the rest of the code
$sql = 'SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, "&#37;d/%m/%Y") AS formattedDate FROM holidays WHERE ';

if ($giveortake == '3' || $giveortake == '5') {
  $useGiveortake = true;
  // We now need to search in a range of dates, using BETWEEN and INTERVAL
  $sql .= 'departureDate BETWEEN "'.$depx.'" AND "'.$depx.'" + INTERVAL '.$giveortake.' DAY';
} else {
  // We need to check for exactly one date
  $sql .= 'departureDate = "'.$depx.'"';
}

if($duration=='7' || $duration=='3') {
  $sql .= ($useGiveortake ? 'AND ' : '') .'duration='.$duration;
}

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
  outputProduct($row);
} 

Otherwise it won’t work if you supply a $duration but no $giveortake.
the ? in $useGiveortake ? 'AND ’ : ‘’ is known as the ternary operator, read about it here.

Thanks ScallioXTX for the clarification and explanation on this. I havent been on my pc yet. i will go through it again and pass you any feedback

Thanks again

Thankx Scallio XTX. It works now. but the destination part (
destination like ‘%$dest%’) is missing from the main query . The destination is not a condition but should be excecuted as part of the main query.

I tried adding it to the Where part in the main query and then added AND to each condition but it generated this error like (Cnnot divide by Zero)

any ideas?
thanks

Sounds like you messed up the string concatenation (again).
Can you post the complete code?

The only 2 things I added were:

  1. destination like ‘%$dest%’ at the end of the main query
  2. The AND part in the condition bit

$sql="SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, price,buyurl, departureDate from holidays WHERE departureDate = ‘$depx’ and destination like ‘%$dest%’ ";

$sql .= ’ AND departureDate BETWEEN “‘.$depx.’” AND “‘.$depx.’” + INTERVAL ‘.$giveortake.’ DAY’;} else { // We need to check for exactly one date $sql .= ’ AND departureDate = “‘.$depx.’”';}

Why is “departureDate = ‘$depx’” back in the main query!? The whole point of my last 10 posts was to get it out of there!
It seems like you’re copy/pasting random snippets from my posts into your own script. Please use my complete script to make sure you incorporate all changes and don’t miss any.

Did you also change


if($duration=='7' || $duration=='3') {
  $sql .= ($useGiveortake ? 'AND ' : '') .'duration='.$duration;
}

to


if($duration=='7' || $duration=='3') {
  $sql .= ' AND duration='.$duration;
}

(i.e., remove the ($useGiveortake ? 'AND ’ : ‘’) part)

and have you also removed the line:


$useGiveortake = true;

since you don’t need it anymore?

Sorry, I already made thoses changes. i forgot and posted the wrong one on here. I didnt include the departureDate in the query and I did remove the $useGiveortake ? 'AND ’ : ‘’) part as well

Ok this is the code now:

$sql = 'SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, “%d/%m/%Y”) AS formattedDate FROM holidays WHERE destination like ‘%$dest%’ ';

if ($giveortake == ‘3’ || $giveortake == ‘5’) {

$sql .= ’ AND departureDate BETWEEN “‘.$depx.’” AND “‘.$depx.’” + INTERVAL ‘.$giveortake.’ DAY’; }
else {

$sql .= ‘departureDate = "’.$depx.‘"’; }

if($duration==‘7’ || $duration==‘3’) { $sql .= ’ AND duration='.$duration;}

$result = mysql_query($sql) or die("Query error: “.mysql_errno().”: ".mysql_error());

while ($row = mysql_fetch_array($result))
{

outputProduct($row)
;
}

-----------------------------end-----------------------------------------

and this is the error message:

Warning: Division by zero in D:\inetpub\vhosts\holidays.php on line 71
Query error: 1064: 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 ‘AND departureDate BETWEEN “2010-05-28” AND “2010-05-28” + INTERVAL 3 DAY’ at line 1

Note: I dont get this error message when I remove the destination part in the main query

Thanks

I will show you the first line of your script in color hightlighting, you should be able to figure out from that what’s wrong:


$sql = 'SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, "&#37;d/%m/%Y") AS formattedDate FROM holidays WHERE destination like '%$dest%' ';

See how the % characters turn green and $dest turns blue? They’re not supposed to do that, they’re supposed to be red, as they need to be a part of the string.

It has to with double quoted strings, single quoted strings, and string concatenation, which I explained to you in post #29

I know your point: I did the double quotes but still getting the same error message:

$sql = ‘SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, “%d/%m/%Y”) AS formattedDate FROM holidays WHERE destination like "’%$dest%'" ';

I can see anything wrong with the code now. I did exactly as you mentioned. I am still getting that error message. Any ideas?

---------------code---------------------------------------------------
$sql = ‘SELECT distinct destination, accomodation, resort, departurepoint, returnDate, duration, type, DATE_FORMAT(departureDate, “%d/%m/%Y”) AS formattedDate FROM holidays WHERE destination like "’%$dest%'" ';

if ($giveortake == ‘3’ || $giveortake == ‘5’ || $giveortake == ‘7’) {

$sql .= ‘departureDate BETWEEN "’.$depx.‘" AND "’.$depx.‘" + INTERVAL ‘.$giveortake.’ DAY’; }
else {

$sql .= ‘departureDate = "’.$depx.‘"’; }

if($duration==‘7’ || $duration==‘3’) { $sql .= ’ AND duration='.$duration;}

$result = mysql_query($sql) or die("Query error: “.mysql_errno().”: ".mysql_error());

while ($row = mysql_fetch_array($result))
{

outputProduct($row)
;
}