Page 1 of 1
Code doesn't work, what's wrong?
Posted: Sat Dec 23, 2006 9:06 am
by arukomp
Hi,
I want to add date into MySQL which is 15 later after now. I tried this way, but I only get errors:
Code: Select all
$pass = crypt($pass1);
$sql = "INSERT INTO users(username,password,name,email,upgrade) VALUES('$username','$pass','$fullname','$email', DATE(NOW() + INTERVAL 15 DAY))";
$result = mysql_query($sql);
Can someone help me please? I must have my site finished tomorrow.
Thanks very much
Posted: Sat Dec 23, 2006 9:39 am
by volka
arukomp wrote:but I only get errors:
You might want to share the error message with us
Code: Select all
$result = mysql_query($sql)or die(mysql_error().': '.$sql);
Posted: Sat Dec 23, 2006 9:41 am
by RobertGonzalez
MySQL date and time functions. Click that link, do a find (ctrl-f) for the word 'INTERVAL' and you'll run across an example like this...
Code: Select all
SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);
#I modified this to use now instead of the string date
Since you didn't tell us your error I can only assume your error is a syntax error, which it appears to be with the way you are incorrectly using the INTERVAL with DATE to add. You also did not mention your MySQL version so I am not sure if you are running 3, 4 or 5, although both DATE_ADD and ADDDATE are supported since 3.23/4.1.
A search of the MySQL manual would certainly be helpful in this situation.
PS Please use descriptive titles when posting threads in our forums. It helps us help you better. Thanks.
Posted: Sat Dec 23, 2006 9:49 am
by arukomp
I looked more carefully in MySQL reference, fixed query and now I have this:
Code: Select all
$sql = "INSERT INTO users(username,password,name,email,upgrade) VALUES('$username','$pass','$fullname','$email', DATE_ADD(CURRENT_DATE, INTERVAL 15 DAY))";
In my computer there's MySQL 5 version I think and in my server (hosting) there's 4.1 version. On my computer this query gives me errors, that there are some problems in syntax. I tried to upload into server and run it and it works

I've always looked at 4.1 version reference and tried to run script on my computer. That's why I always got this error
Thanks anyway for your answers

Posted: Sat Dec 23, 2006 10:57 am
by volka
I really don't think it's a version problem on your PC (having the higher version)
I tried
Code: Select all
<?php
$db = mysql_connect('localhost', 'localuser', 'localpass') or die(mysql_error());
mysql_select_db('test', $db) or die(mysql_error());
$username='';
$pass='';
$fullname='';
$email='';
echo 'server: ', mysql_get_server_info($db), "\n",
'client: ', mysql_get_client_info(), "\n";
$sql = "INSERT INTO users(username,password,name,email,upgrade) VALUES('$username','$pass','$fullname','$email', DATE_ADD(CURRENT_DATE, INTERVAL 15 DAY))";
mysql_query($sql, $db) or die(mysql_error().': '.$sql);
$sql = "INSERT INTO users(username,password,name,email,upgrade) VALUES('$username','$pass','$fullname','$email', DATE(NOW() + INTERVAL 15 DAY))";
$result = mysql_query($sql, $db) or die(mysql_error().': '.$sql);
echo 'done.';
the output was
server: 5.0.27-community-nt
client: 5.0.22
done.
no error, no warning
Posted: Sat Dec 23, 2006 11:54 am
by arukomp
Mine output for MySQL version:
Code: Select all
server: 5.0.24a-community-nt
client: 5.0.24a
And I didn't change the code, on my computer it didn't work, but on the server it worked...
Posted: Sat Dec 23, 2006 12:18 pm
by volka
Since the error mesage is obviously a secret I'm just happy it all worked out for you.
Posted: Sat Dec 23, 2006 2:36 pm
by RobertGonzalez
Ok arukomp, help us help your. On the system that is failing, please tell us the exact error message you are getting back AND do a echo $sql; so we can the exact SQL command that the database is seeing. That would go a long way in helping us figure out this issue for you.