Page 1 of 1
Questions (Ima noob)
Posted: Wed Jan 07, 2009 7:25 am
by Randwulf
I just began my foray into PHP and MySQL (meaning you'll see more of me, so hi

) and my first project was to build a guestbook. That's done, but it left me with some questions. I apologize if any of them are annoyingly noobish.
1. I'd imagine it would be quicker for simple things such as guestbooks to have PHP edit the html file directly upon receiving an entry (assuming that's even possible) rather than generating the page from the database every time someone accesses it. Is this correct?
2. I have a whole table in my database set up just for counters. My guestbook app posts the user's information to one table with the counter's value as the key value, and then decrements the counter value on the counter table by one. This way, to display the entries, PHP reads the counter and executes a for loop that iterates through all of the posts and echos them to the page beginning with the newest and ending with the oldest. I'm almost positive there's a better way to do this.
3. I have the date fetched with date('m/d/y') and then saved to a date column. I've tried different types of columns but have gotten various problems. I want the date saved as mm/dd/yy but instead it's saving as 20mm-dd-yy. So today would be 2001-07-09. I can fix this by only outputting substr($date,2,8) but I'd rather it were more efficient of course.
Thanks for reading.
Re: Questions (Ima noob)
Posted: Wed Jan 07, 2009 7:30 am
by onion2k
Randwulf wrote:1. I'd imagine it would be quicker for simple things such as guestbooks to have PHP edit the html file directly upon receiving an entry (assuming that's even possible) rather than generating the page from the database every time someone accesses it. Is this correct?
Quicker to display the results, sure, but much, much slower to update the data. Besides, we're talking fractions of a second to display a list of things from the database, so unless you're getting millions of views it's not a problem.
Randwulf wrote:2. I have a whole table in my database set up just for counters. My guestbook app posts the user's information to one table with the counter's value as the key value, and then decrements the counter value on the counter table by one. This way, to display the entries, PHP reads the counter and executes a for loop that iterates through all of the posts and echos them to the page beginning with the newest and ending with the oldest. I'm almost positive there's a better way to do this.
That sounds weird. Why not just have an id for each post in the guestbook?
Randwulf wrote:3. I have the date fetched with date('m/d/y') and then saved to a date column. I've tried different types of columns but have gotten various problems. I want the date saved as mm/dd/yy but instead it's saving as 20mm-dd-yy. So today would be 2001-07-09. I can fix this by only outputting substr($date,2,8) but I'd rather it were more efficient of course.
You should always save dates in DATE columns. If you need the date in a specific format use MySQL's DATE_FORMAT() function, or PHP's date() function if you prefer to do that sort of manipulation on the code side (database is better mind you).
By the sounds of things you're worrying about the efficiency of your code before it's actually a problem. Don't. Wait until it's an issue because 99.9% of the time it never will be.
Re: Questions (Ima noob)
Posted: Wed Jan 07, 2009 8:14 am
by Randwulf
onion2k wrote:
Randwulf wrote:2. I have a whole table in my database set up just for counters. My guestbook app posts the user's information to one table with the counter's value as the key value, and then decrements the counter value on the counter table by one. This way, to display the entries, PHP reads the counter and executes a for loop that iterates through all of the posts and echos them to the page beginning with the newest and ending with the oldest. I'm almost positive there's a better way to do this.
That sounds weird. Why not just have an id for each post in the guestbook?
I do kind of. The counter number is the ID/Key number. But is there a better way to assign unique numbers to posts than having a counter somewhere in the db that attaches itself to the post and then increments or decrements by 1 so that the next post gets a unique number too? EDIT: I found what I was looking for. It was a pretty dumb overlook actually.
Ty for clearing up the speed thing.
Is it always faster to have the db do things (like format dates, that sort of thing) when possible rather than the PHP?
Re: Questions (Ima noob)
Posted: Wed Jan 07, 2009 9:16 am
by onion2k
Randwulf wrote:Is it always faster to have the db do things (like format dates, that sort of thing) when possible rather than the PHP?
No idea. Don't care either. Execution speed for that sort of thing is of absolutely no consequence until you're getting tens of millions of users every day. My sites only get a couple of million
a month so I've never had any reason to think about it.
Re: Questions (Ima noob)
Posted: Wed Jan 07, 2009 12:51 pm
by Randwulf
onion2k wrote:Randwulf wrote:Is it always faster to have the db do things (like format dates, that sort of thing) when possible rather than the PHP?
No idea. Don't care either. Execution speed for that sort of thing is of absolutely no consequence until you're getting tens of millions of users every day. My sites only get a couple of million
a month so I've never had any reason to think about it.
Lol, ok, that's fair. Thanks for your help.