Kevin3374 Posted March 11, 2008 Share Posted March 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/ Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 try <?php $query_str = "SELECT UNIX_TIMESTAMP(start_date) as start_date, UNIX_TIMESTAMP(end_date) as end_date, title, location FROM Events"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489039 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Try using {$endDates[{$x}]} If that fails, print_r() and make sure your arrays actually have data in them. Everything else looks fine Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489041 Share on other sites More sharing options...
Kevin3374 Posted March 11, 2008 Author Share Posted March 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489056 Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 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)']}"; Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489066 Share on other sites More sharing options...
Kevin3374 Posted March 11, 2008 Author Share Posted March 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489098 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 I don't believe there is one. Quote Link to comment https://forums.phpfreaks.com/topic/95534-question-regarding-unix-timestamps-php-and-mysql/#findComment-489103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.