Exactly . . .
I recently got a new charger for my iPhone and it was working perfectly. However, it stopped charging when I plugged it into my macbook one day. The solution for my issue was simple however, at the end of the post is a link to detailed discussion on how to fix if your iPhone not charging at all. This will fix issue with iphone 3g not charging, 3gs having issues with charging and iPhone 4 failing to charge too.
Summary of solution:
Restart computer and iPhone
Detail of solution:
I simply restarted my computer
then I restarted my iPhone (also did a power + home button for 15 seconds reboot)
and it all went perfectly fine!
Usually its just the computer which stops charging the device. Stops sending current through the usb device due to any reasons, e.g too much system load, on and off system sleep mode, etc. So simply removing the charger cable from iphone and laptop both, and simply restarting them will solve the issue in most cases.
However, if you’re still not able to fix your iPhone and it’s still not charging, then follow the steps outlined in this post: Iphone not charging
Update: (thanks to Sanaullah)
Launching iTunes while iphone is connected to the computer fixes the iphone charging issue too in some situations.