A Better PHP/MySQL Connection

No Comments »

Keeping heavy loads off of a database can be a tedious task in itself.  Many times multiple instances are created, creating multiple connections to the database that hog resources when in fact only one instance and connection is needed.  With PHP5, database abstraction layers are built into the core.  PDO, although it has it’s deficiencies, can be a good starting point for creating a simple database abstraction layer.  However, learning how to use one single instance throughout the life of the page is the key to balancing server load and enhancing performance.

Starting off with a simple “autoload” file I find is helpful.

<?php
include( ‘autoload.php’ );
?>

The autoload file doesn’t have to be complicated.  It can be a basic file that creates all instances to be used through out the page, not to mention autoload classes that can be called else where throughout the script.

autoload.php

<?php
include( ‘config.php’);

// loop to auto load class files

/* Single Database initiation to be used throughout the script */

$dsn = “mysql:host=localhost;dbname=my_database”;
$dbuser = “my_database_username”;
$dbpass = “my_database_password”;

$database = new PDO( $dsn, $dbuser, $dbpass );
?>

Typically it is better to define the database info in a global configuration file that will typically not change.  It keeps all important variables that will be used multiple times in one place.  But this autoload file defines the variable $database that will be used throughout the rest of the page.  This will create on instance and most importantly, 1 connection to the database to be used.

On the file that will be needing, not mention allowing class and objects to use this instance can be accomplished this way:

File:

<?php
include( ‘autoload.php’);

$sql = “select * from my_table”;

$stmt = $database->prepare( $sql );
$stmt->execute();

while( $rows = $stmt->fetchAll() )
{
// Do Something
}
?>

Passing and allowing usage in objects:

<?php
include( ‘autoload.php’ );

$helper = new helper( $database );

echo $helper->displayView();
?>

The class file:

<?php
class helper
{
private $database;

public function __construct( $database )
{
$this->database = $database;
}

public function displayView()
{
// do something
$sql = “some sql statmet”;
$stmt = $this->database->prepare( $sql );
$stmt->execute();

while( $rows = $stmt->fetchAll() )
{
// Do something
}
}
?>

As you can see, this allows one single database thread to be used across the entire page freeing resources on the server making it more efficient.

For more information on PDO visit the manual at http://www.php.net/pdo.

Add This! Blinkbits Blinklist Blogmarks BlogMemes BlueDot BlogLines co.mments Connotea del.icio.us de.lirio.us Digg Diigo DZone Facebook FeedMeLinks Folkd.com Fleck Furl Google Google Reader icio.de IndianPad Leonaut LinkaGoGo Linkarena Linkter Magnolia Mister Wong MyShare Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Netvouz Newsgator Newsvine Oneview.de RawSugar reddit Rojo Segnalo Shadows Simpy SlashDot Smarking Sphere Spurl Startaid StumbleUpon TailRank Technorati ThisNext yigg.de Webnews.de ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com Plugin by Dichev.com
PHP, Tutorials, Web Development, Web Programming | June 24th 2008

Web Validation

No Comments »

For my first post I figured I’d write about the W3C validation. I was reading on a forum today where the question “Should I put the W3C validation image on my site?” Most of the responders, as do I, agree that image is horrifyingly ugly. However the question of should you validate came up. It is true that many of your clients don’t care if it validates as long as it works. It was also brought up that huge companies such as Yahoo! do not validate their code.

My opinion, I believe validation should be done, however may not always be plausible. Unfortunatly, Firefox and Internet Explorer render CSS and XHTML very differently. What is built in and works in Firefox, will not always work in IE. Some hacks are necessary to achieve the same affect. Now IE 6+ does not even have support for XHTML. I like to code using XHTML. Unfortunately for me, it will not render in IE 6+.

The problem with this, my clients, as I’m sure your’s, will want the site to return in all browsers. Even if it validates, it really does them little good if a user is returning a blank site in their browser. IE has frustrated me for years because of its CSS and XHTML rendering abilities. Basically, it has none. So it makes you question, is validation worth it? W3C has created certain rules and guidelines to ensure your website will render properly in all browsers. Apparently, Microsoft did not get this memo when creating IE 6+.

However, being a code nazi as I am, I cannot stand code heavy pages that looks like they just threw a bunch of tables together, used Photoshop to cut up the layout, and slapped it together with some tape. I think it is the absolute worse thing a web developer can do. Personally I like super light pages with just enough code to put it together. Granted, creating a true CSS website is not exactly easy to do, it has caught on in the recent years. I was fortunate enough to discover the ideas of Dave Shae and his project on CSS Zen Garden early on in my career. I actually had the opportunity to work with Dave, unfortunately I did not.

Ever since discovering CSS Zen Garden, it has inspired me to create websites in a whole new light. Granted he is for validating your code. And I by no means am against it. I believe it should be done with the exception of certain instances that it is not in the site’s best interest. I think there are many benefits to it besides being able to brag that your site is W3C compliant. Google likes it better, your code to content ration is a lot lower, giving the impression to the search engines spiders that your site has tons of information, design images are where they belong, in the stylesheet and not in the page itself, leaving only the necessary images on the physical page, not to mention, the code is so much easier to work with and readable.

Many in the forum believe that it should be “assumed” that the developer created the site and validated it. From my experience, many developers probably have no idea what the W3C is, or what their validation means. Looking at a lot of theses sites, sure the layout might be nice, but the same site can be done with pure CSS. I’ve been told by many developers “OH you can’t do a pure CSS page with this layout!” Yet creativity has proven time after time, that I can take any design you give me, and I will create an entirely code light pure CSS page with it. So it can be done. But in my recent years, I have not validated as much as I should be doing. However I still believe that all sites should be as compliant as possible, and that it is an important thing to do. Why call a kid a “noobie” just because he takes the time to validate his work. Granted I wouldn’t put the image on my site, but I feel atleast he is headed in the right direction with his web development career.

Add This! Blinkbits Blinklist Blogmarks BlogMemes BlueDot BlogLines co.mments Connotea del.icio.us de.lirio.us Digg Diigo DZone Facebook FeedMeLinks Folkd.com Fleck Furl Google Google Reader icio.de IndianPad Leonaut LinkaGoGo Linkarena Linkter Magnolia Mister Wong MyShare Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Netvouz Newsgator Newsvine Oneview.de RawSugar reddit Rojo Segnalo Shadows Simpy SlashDot Smarking Sphere Spurl Startaid StumbleUpon TailRank Technorati ThisNext yigg.de Webnews.de ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com Plugin by Dichev.com
CSS, Web Development, XHTML | May 21st 2008