Code doesn't work, what's wrong?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Code doesn't work, what's wrong?

Post 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
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post 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 :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post 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...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Since the error mesage is obviously a secret I'm just happy it all worked out for you.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply