I have my home page and I want to display large images there from site members. I want to ensure that each image is displayed the same amount of time as all the other images. I could use a database for this but feel that is a slow method especially IF I get lots of traffic.
Anyone have any thoughts. Loading and modifying a text file. Not sure what the locking issues are on text files if any.
I am not going to give you a solution because StarLion gave you a nice one.
BUT if you think at “Text file” vs “database” - the database will win: speed, structure, security
With a database you will also keep track of your views, clicks and others (important features for any marketing department)
Generally databases will be faster, since they are way more advanced at reading and writing disk, plus they keep caches to speed things up.
Yes, MySQL has both, but I don’t have any extensive experience with them, so I can’t help you out there.
Another thought to use would be Memcache. Just use the principle outlined by @StarLion; above, but read from and write memcache instead of a file. Since memcache is purely in main memory it probably won’t get any faster than that.
I just thought an SP would be better. Doing it all in one instead of 2 calls (Select,Update) to the database. The speed is marginal so will go for the DB version. I don’t really need to check clicks to a certain degree as the banners will be placed on page using an google analytic event.
If you’re going to use MySQL for the database, please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.
If any of the code for your ad-rotator is going to send anything to the database, from any user-submitted source (cookies, get, post, etc) then you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.
I’ll throw a caveat onto this result; the example data shown here is for a LOCAL database. If your provider hosts their database servers remotely (like GoDaddy does), this will affect the overall time of retrieval.
Ok, I didn’t know about this approach.
However, this is just a hosting “bad practice” (I will not debate this, maybe there are reasons like security and others) but as programming it’s nothing you can do about it.
Still, I will keep my idea that “database is better!”
edit // About what SpacePhoenix said, I use PDO (and I recommend it) but I used core functions to make it as fast as possible.
This was just to prove my point, I don’t use mysql_* since PDO appeared.