Log in

View Full Version : PHP/MySQL "Best Practices"



FlashbackJon
04-05-2008, 07:12 PM
I'm still a bit of a database n00b here, but I've been developing a small web-based game for my friends using PHP and MySQL for a little while, and this question has been bothering me for a little while:

Is it better to query the database once and then use PHP to "parse" the data each time it needs to find something, or to run a series of smaller, much more specific queries littered throughout the page when it needs it?

I.E., I have a table with maybe 150 records on it, of which I will need all of them over the course of a page. Is it better to query the whole table and then let PHP run a foreach or while loop to find the 3 or 4 records it needs each time it needs them? Or is it better to run a specific query each time it needs those 3 or 4 records and then just dump the data directly?

I should clarify that by "better" I'm referring to both standards-compliancy and resource consumption. If this game grows as I hope it will, I don't want to be slamming my poor little database. :D

Nile
04-06-2008, 12:36 PM
I think that the while loop will be your best choice. :D