I don't see anything immediately wrong with your code. The only thing I can think of is perhaps it is only returning one message so the foreach is pointless. You could try doing a quick check to see if the $messages var is an array or just a single value. If it's an array then foreach them and print them out but, if it's just a single value then skip the foreach and just print it. That's more of a tip than anything I guess. Not sure if it will solve your problem. Doing it that way would make your code more efficient though. Then PHP doesn't have to call up all the foreach stuff for a single value (which is overkill).
One thing you should check is if your code is spitting out any PHP errors. If you have errors disabled on your server then I'm not sure what you have to do to see the errors. In a log somewhere I would assume. But, if you can find any errors pertaining to your code, that might help us if you post it.
Thanks for your reply. I managed to correct the error. I'm trying to create the script to be able to view the images after been loaded in database.The same concept when we posted how messages in forum. Do you've some guidance? Thank in advance.