I think that its time that I structure my PHP code more efficiently. I mean that I think its time that I move onto OOP(Object Oriented Programming) in PHP, I think that I know enough of PHP by now to venture into the world of OO. 😉
After building my first PHP based website fully powered by MySQL Database(http://www.igeek.info/), I now know that though connecting to MySQL database in PHP is quite easy, it can be further simplified by making re-usable functions to execute the same lines of code instead of writing them again & again. 😀
In any pages that utilise a database connection & do some manipulation be it getting some results or insertion of data or updation etc, it would be foolish to write down the same lines of code(for connecting to database) again & again. I never did that even in ASP, then why should I do it in PHP? 😉 😐
Some feasible solutions are:
~ I put the common part, that is the server & database connection code in a file & include it wherever I need to do database manipulation.
~ Or I make a function where I put all of the above code. I then call that function wherever I need database manipulation & pass the SQL Query to the function as a parameter. Other parameters that can be passed are SERVER, USERNAME, PASSWORD & DATABASE NAME. Now these last 4 parameters need to be passed only if these change frequently in the same website. If these are same for all the website then it is wise to put these values in a global configuration file & then include that configuration file in the functions file.
Second option looks more easy to implement and maintain and is clean. So, we can have a function which contains all the connection code & we just call the function wherever we want & pass the SQL Query as a parameter to it. It then runs that query & gives out a resultset which can be parsed using the mysql_fetch_array() function. 😀
This thing is pretty much OK, but if OOP is kept in consideration, this function can be converted into a class with a single function(or more if necessary) like this & then an object of that class is can be created & the SQL Query can be passed to it.
So, moving along towards OOP, I’ve decided to do that too. But instead of re-inventing the wheel, I’m going to use a ready made class ezSQL which does a lot more than just running SQL Queries. Infact this class is used in WordPress too, the famous PHP Blog script, which incidently, is used on this Blog too. Another thing that I’m gonna try out is EZ Results Paging Class which pages a set of results retrieved from a database using ezSQL. It means that suppose there are 80 records in a resultset but you want to display only 10 records at a time. This class will generate clickable page numbers which you can click to see further results. A popular implementation of this kind of paging can be seen on search engines. They display a fixed number of records on each page & if records fetched are more than that, they display page numbers.
I haven’t tried any of them myself yet, but I do hope that they make my work more easy & efficient. 😀 😉