Jump to content


Photo

PHP/MySQL adddate function


  • Please log in to reply
2 replies to this topic

#1 mwarby

mwarby
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 March 2006 - 12:41 PM

I'm trying to generate a list of dates each one being 7 days on from the last,I then hope to query a database to check if this date has been reserved.

at the moment I'm trying just to get the basic list weh I use

$query='select adddate('2006-02-01',interval 7 day)';
$result=mysql_query($query);
echo mysql_result($result,0);

I get the expected result I tried putting it in a for loop as below

for($counter=1;$counter<28;$counter+=7)
{
$query='select adddate('2006-02-01',interval $counter day)';
$result=mysql_query($query);
echo mysql_result($result,0);
}

but i get
Parse error: parse error, unexpected T_LNUMBER in /home/knott/domains/knottcottage.co.uk/public_html/avail.php on line 17

my complete code is below

<html>
<head>
<title>Availabilty Checker</title>
</head>
<body>
<h1> Availabilty checker </h1>
<?php
$dbhost = 'localhost';
$dbuser = 'knott_book';
$dbpass = '1234';
$database = 'knott_booking';
echo 'test1';
mysql_connect(localhost,$dbuser,$dbpass);
@mysql_select_db($database) or die( "Unable to select database");
for($counter=1;$counter<28;$counter+=7)
{
$query='select adddate('2006-02-01',interval $counter day)';
$result=mysql_query($query);
echo mysql_result($result,0);
}
?>

</body>
</html>

Thanks for reading
Martin

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 March 2006 - 04:27 PM

Change your query to:

$query="select adddate('2006-02-01',interval $counter day)";

With the single quotes on the outside, then using them again for the date, you were escaping the string, which would cause you to get an errror. If it had made it as far as your variable, it would not have replaced the $counter variable with it's value, as the single quotes cause it to take all text literally.

#3 mwarby

mwarby
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 March 2006 - 05:00 PM

[!--quoteo(post=354216:date=Mar 12 2006, 11:27 AM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 12 2006, 11:27 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change your query to:

$query="select adddate('2006-02-01',interval $counter day)";

With the single quotes on the outside, then using them again for the date, you were escaping the string, which would cause you to get an errror. If it had made it as far as your variable, it would not have replaced the $counter variable with it's value, as the single quotes cause it to take all text literally.
[/quote]

Thanks a lot thats that problem sorted :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users