MySQL Archives

Quick Tip: MySQL date_add

Published by David Walsh on Wednesday, May 19, 201010 Comments

Here’s a quick MySQL tip I wanted to throw your way. I created an event system a while back and one of the requirements of the system was to show events that happened yesterday forward, meaning events older than 2 days were to be hidden. MySQL’s date_add will allow you to do just that:

The MySQL Example

SELECT title, venue, url, city, state, DATE_FORMAT(date_starts,\'%b %e\') as formatted_date
FROM events
WHERE date_starts >= DATE_ADD(NOW(), INTERVAL -1 DAY) // yesterday!
ORDER BY date_starts ASC

Note that I’m using a negative date value. You can use a positive date value to get tomorrow, next week, next month, etc..

Create Custom Element Property Getters and Setters with MooTools

Published by David Walsh on Monday, May 3, 20104 Comments

One of the goals of the MooTools projects is to create very flexible methods by which developers may modify or extend the behavior of given classes. One example of that flexibility is the ability to customize the Element class’ get and set methods for specified values.

The default behavior of the get and set methods for an object are to return/modify the given Element attribute. Take the following for example:

var alt = document.id('myImage').get('title');  //returns the image's title attribute text

The above code modifies the title attribute for the given element. Simple, of course, but then MooTools also uses the get/set methods to set and retrieve items that aren’t element attributes:

Create a Basic Web Service Using PHP, MySQL, XML, and JSON

Published by David Walsh on Wednesday, May 6, 200939 Comments

Web services are taking over the world. I credit Twitter’s epic rise to the availability of a simple but rich API. Why not use the same model for your own sites? Here’s how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.

Backup Your Database into an XML File Using PHP

Published by David Walsh on Friday, February 27, 200927 Comments

Backing up data is extremely important. Most of the time the database is the most important piece of the puzzle. Imagine losing all of the data in your database — it would be tragic. Here’s a PHP snippet that outputs your database as XML.

Animated AJAX Record Deletion Using MooTools

Published by David Walsh on Thursday, January 29, 200945 Comments

I’m a huge fan of WordPress’ method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here’s how to achieve that functionality with MooTools JavaScript.

The PHP – Content & Header

The following snippet goes at the top of the page:

if(isset($_GET['delete']))
{
	$query = 'DELETE FROM my_table WHERE item_id = '.(int)$_GET['delete'];
	$result = mysql_query($query,$link);
}

The following is used to display the records:





© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools JavaScript framework.