Home arrow PHP arrow Page 4 - Improving MySQL Connection with Static Variables in PHP 5 Classes

A final example - PHP

In this third part of a four-part series on static variables, I show how to use another static property in the previous MySQL driver to connect “more cleverly” to the database server. This example demonstrates how powerful a property like this can be, when utilized in an effective manner.

TABLE OF CONTENTS:
  1. Improving MySQL Connection with Static Variables in PHP 5 Classes
  2. Review: the previous MySQL driver's classes
  3. Connecting to MySQL only once
  4. A final example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 29, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate the actual functionality of the $_connected static property just added to the "MySQLiWrapper" class, below I wrote a script that runs two trivial queries against the same sample "users" table. In this case, the class will happily connect to MySQL only once, which is the expected behavior. 

The script looks like this:

<?php

 

try

{

// connect to MySQL

$db = MySQLiWrapper::getInstance(array('host', 'user', 'password', 'database'));

 

// fetch users from database

$users = $db->runQuery('SELECT * FROM users');

 

// display user data

foreach ($users as $user)

{

echo 'First Name: ' . $user->fname . ' Last Name: ' . $user->lname . '<br />';

}

 

// fetch users' first names from database

$users = $db->runQuery('SELECT fname FROM users');

 

// display user data

foreach ($users as $user)

{

echo 'First Name: ' . $user->fname . '<br />';

}

 

}

// catch exceptions

catch(Exception $e)

{

echo $e->getMessage();

exit();

}

There you have it. Thanks to the inclusion of a static variable, now any object spawned from the previous "MySQLiWrapper" class will establish only a single connection to MySQL. Hopefully, this final example will spark your own inspiration and get you started using static properties in all sorts of creative ways.

Final thoughts

In this third part of the series, I showed how to use another static property within the previous MySQL diver to connect "more cleverly" to the database server. Once again, this example demonstrated how powerful a property like this can be when utilized in an effective manner.

In the last chapter of the series I'm going to teach you how to get the same benefit when connecting to MySQL via a static, local variable, instead of explicitly using a class property.

Don't miss the upcoming tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: