Jump to content

PHP/MySQL adddate function


mwarby

Recommended Posts

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
Link to comment
Share on other sites

Change your query to:

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

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.
Link to comment
Share on other sites

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

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

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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.