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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: