Jump to content

Archived

This topic is now archived and is closed to further replies.

mwarby

PHP/MySQL adddate function

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

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

×

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.