Jump to content

Recommended Posts

I'm having some trouble inserting timestamps into a mysql table.  I am inserting entries into a table using the following query:

 

$query = <<<SQLQUERY
INSERT INTO Events
  		SET
  		start_date = FROM_UNIXTIME($startDates[$x]),
  		end_date = FROM_UNIXTIME($endDates[$x]),
  		title = "$titles[$x]",
  		location = "$locations[$x]"
SQLQUERY;

 

Note that start_date and end_date were created with type DATE, in mysql. 

 

In another file, I am trying to select and display the records from this table using the following code

 

$query_str = "SELECT UNIX_TIMESTAMP(start_date), UNIX_TIMESTAMP(end_date), title, location FROM Events";
$result = $conn->query($query_str);

//left out a few lines here

while (($row_data = @$result->fetch_assoc()) !== NULL)
	{
		echo "<tr><td>{$row_data['start_date']}";

		if($row_data['end_date'] != NULL)
			echo " - " . date('M d', $row_data['end_date']). "</td>";
		else
			echo "</td>";	

		echo "<td>{$row_data['title']}</td><td>{$row_data['location']}</td></tr>\n";	

	}//end while

 

I get the title and locations back correctly, but the cells for the dates all come back empty.  Anyone have any idea what I'm doing wrong?

try

 

<?php
$query_str = "SELECT UNIX_TIMESTAMP(start_date) as start_date, UNIX_TIMESTAMP(end_date) as end_date, title, location FROM Events";
?>

 

Thanks, that worked perfectly.  Not sure why my book said to do it the way I had it, but your way definitely works.

please click topic solved

 

without the "as" you would of needed to do this

echo "the start date is {$row_data['UNIX_TIMESTAMP(start_date)']}";

 

Okay, I'm sure its somewhere really obvious and I'm just blind.  But I don't see a 'topic solved" link anywhere.  I even did ctrl-f and the only result was in your reply to me.

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.