Persistent MySQL connection

February 23, 2010

It was not that long ago I figured out what this ment, even thou persistent is kind of self-explanatory. I read that this was a must on a sql connection, but I never learnt it at school or did find out how to do it. For me it was a fancy word for a more complex connection type, but is it really more complex?

What I usually do, is that I use the singleton pattern on my database connection so that on each load the connection is created only once and then reused for every new use. A database connection is expensive, so it is a good thing not to create it a lot, and this is what singleton does. A persistent connection is like a singleton to the server – and does the thing that I hoped singleton would do when I first heard about it.

A persistent connection does not close, but it reused by the different connections to the server. This means that the same connection is reused by every user that connects to the server, and there is not created a new one for each time. Sounds simple? It is simple.

The real use of it is probably only when you have a bigger site with many users and traffic is big, so it is not really necessary for small websites that barely use a connections and have under 50 visitors a day. The singleton helps a lot on each user page load, when the connection is preserved and reuses for the page load, but still, using a persistent connection could slightly improve speed even thou it is probably not noticeably. The real problem solved with persistence is that you can have many more simultaneous connections to the database – getting rid of the connection overhead problem.

How to do it?
I use the mysqli extension for PHP, and the only thing needed is to add a ‘p:’ to the host address, like this:

$db = new mysqli('p:localhost', 'username', 'password', 'databasename');

This only works with PHP 5.3.0 or higher, so be sure you have it both locally and on the server. I you don’t have it or don’t use mysqli, then there is an alternative, to switch form mysql_connect to mysql_pconnect.

That simple, really – and no other change to code.

Advertisements

How to set that title tag?

February 15, 2010

I’m in that place in developing my new site that I have to decide how I will manage the inclusion of pages, and there is a problem what I’ve really never bother about before. How to change the title tag in HTML the best possible way. This is a must for several reasons. First off, it makes the SEO(Search Engine Optimisation) better by giving a descriptive title to the page that show up in results. Secondly, most browsers now use tabbed browsing and to se on the tab what page it is you are on is a good thing.

Now, I have to tell that I do not use frameworks – mainly because I want to learn stuff myself first – so even thou they probably have implemented a good solution, I still need to fine one myself that will satisfy my needs and make the programming part good.

The problem might not be a big problem, but it is really a good example on why a good design should be implemented. The way I have done it before is simply to have one title for each page – because the title is in the header, and the header was an HTML file I included at the top of the php. Easy fix is to add some php that gets the page attribute from the url, and then does an if else on every page to give the right title. The problem is, this is an if else, or switch, that you then probably have to implement a lot of times in the code – for example to find out which menu item you are currently on – and makes the code unneccessary hard to read. The rule of making most of the logic in a underlaying architectural layer should apply.

So, the solution then? Well, I tell you my simple plan that is under developement.

I’m going to make the page a PHP Object, (pseudocode) like this:

$page = new Page($_GET['page']);

The “?page=user” is set in as an attribute to the page object. The same is done with the id=1, and other attributes if they exists. Then when the page is printed the header is put together.

$page->printHTML();

printHTML() is using the self::getHeader and self::getBody to build the complete HTML wrapped in Should Body and Header be Objects too?
Should Footer, mainBody, navBar, Logo, sideBar be objects as well?

To be honest, I really don’t know how far I will make it – but I do se a pattern that every changeable part of the design should be objects – so that they can be built on they own. Especially Header, Body and sideBar(that is a part of the body). This is a bit out of scope right now, but the point is to make a design that is object-oriented right to the core, making the attributes on the object decide what HTML to include, and then present it the right way.

Back to the title, lets imagine we have a simple Page object with a simple all in one getHTML() function. This then contain the following simplified code:

return ''.self::getTitle().''.self::getBody().'';

The getTitle() function should do something like this:

$title = 'Sitename - '.$this->page; //User
if($this->id){
$user = new User($this->id);
$user->get(); //Get the user from database
$title .= ' - '.$user->getName();
}
return $title;

This is not the code I’m going to use, it’s just to show the logic. I think this approach gives good practise in programming an OO(Object Oriented) way, and hopefully makes the site flexible too.

Does this make any sense at all?


Starting out real slow

February 14, 2010

Well, since I wanted a tool to share discoveries in programming, I jumped in to this WordPress paltform. Not sure why the lot fell on WP, but it did, and hopefully my new relationship will last. If you read this, don’t worry if this is the only thing you read – because it really is the first post I do, and it may be days until the next. I want to start out slow, and write when I’m inspired to do so. I have blogged for years so I kinda know the drill, and hope that I’m going to manage this new one.

New platform, resulting in a lot of new tweaking possibilities to discover, but I’m not going to do that at first. I used one of the themes I found on the list, and are going to use themes from there, because I don’t have to much freetime to use on fixing an own personal one.