Jump to content

[SOLVED] Won't let me run time arithmetic inside VALUES ( )?


SapAuthor

Recommended Posts

I'm at a complete loss.  I'm trying to make a simple prayer request program.  There are 3 times inside the MySQL database.  The Creation/modification time, the reminder time (current time + X amount of days), and the experation time (current time + X amount of days + 30 more days).

 

Here's what I have:

 

$mysqli = mysqli_connect('localhost', '*******', '*********', 'prayer_request');
$name = $_POST['request_name'];
$email = $_POST['email_address'];
$request = $_POST['the_request'];
$password = $_POST['the_password'];
$dayreminder = $_POST['reminder_days'];
$experation = $dayreminder + 30;

$sql = "INSERT INTO prayers (answered, date_time, date_remind, date_expire, name, email, request, password) 
VALUES ('n', now(), (CURRENT_DATE() + INTERVAL $dayreminder DAY), (DATE_ADD(NOW(), INTERVAL $experation DAY)), '$name', '$email', '$request', '$password')";


$res = mysqli_query($mysqli, $sql);
if ($res === TRUE)
{echo "The data was entered successfuly!";}
else
{printf("Could not insert record: %s\n", mysqli_error($mysqli));}

 

Now i've tried a lot of things.  from just "Now() + INTERVAL $dayreminder DAY" to "(NOW() + INTERVAL $dayreminder DAY" to DATE_ADD(CURDATE(), INTERVAL $dayreminder DAY)".  NOTHING works.  I either get the same date as now(), or i get 00:00:00.

 

What am I doing wrong?  How do i get it to output it correctly?

 

--------- -----------

 

Next question.  I could try to do this outside of MySQL and get things ready in PHP.  I know i can just use Timestamp and add the certain amount of seconds, reput it in the right format and then plug in that variable for the query, but is there an easier way to do it?  What i mean is, can i assign a simple Select Query that outputs one thing like the date to a php variable?  Instead of:

$thequery = "SELECT CURRENT_DATE() AS thevalue";
$res = mysqli_query($mysqli, $thequery);
$newArray = mysqli_fetch_array($res, MYSQLI_ASSOC);
echo $newArray['thevalue'];

 

if there is a shorter way i would like to learn it because i'll be doing a lot of coding coming up...i'm still learning so sorry for the noobism, i couldn't find ANYTHING on google about this >.>

Link to comment
Share on other sites

Oh...my...sweet...Jesus....

 

It just occured to me that I set "Time" instead of "date" as the column type, in my dilusionous sleep deprived 3am coding state of mind.

 

This is most likely why it showed 00:00:00 when i added dates...

 

*tested*...yeah...that was the problem...ugh...

 

Sorry the people that viewed this and posted.

 

----------------

 

Also still, is there any shorter way to retrieve simple values from MySQL, such as the date, or the length of a string?  or do you have to take the result of the mysqli_query()  and put it through the mysqli_fetch_array?

Link to comment
Share on other sites

Also still, is there any shorter way to retrieve simple values from MySQL, such as the date, or the length of a string?

How do you mean? Oh, maybe just a single column output? You can use mysql_result( $result, 0, 0 ) to get back, say, a COUNT().

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.