Home arrow PHP arrow Page 3 - Storing Class Properties of Persistent Objects in MySQL Tables

Creating a MySQL abstraction class - PHP

Welcome to the fifth part of a six-part series that shows you how to build persistent objects in PHP 5. In this part of the series, I'll show you how to develop a persistent class that can save its properties to a MySQL table.

TABLE OF CONTENTS:
  1. Storing Class Properties of Persistent Objects in MySQL Tables
  2. Review: saving class properties to a text file
  3. Creating a MySQL abstraction class
  4. Building a database-driven persistent class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 29, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

While this may sound obvious, to build a persistent class that will save its properties to a MySQL database table, it's first necessary to create an interface that permits it to access that table in a straightforward way. In this case, the element that will play that role will be a basic MySQL abstraction class whose definition will look as follows:

class MySQL

{

private $result = NULL;

private $link = NULL;

 

// connect to MySQL

public function __construct($host, $user, $password, $database)

{

if (FALSE === ($this->link = mysqli_connect($host, $user, $password, $database)))

{

throw new Exception('Error : ' . mysqli_connect_error());

}

}

 

// perform query

public function query($query)

{

if (is_string($query) AND !empty($query))

{

if (FALSE === ($this->result = mysqli_query($this->link, $query)))

{

throw new Exception('Error performing query ' . $query . ' Error message :' .mysqli_error($this->link));

}

}

}

 

// fetch row from result set

public function fetch()

{

if (FALSE === ($row = mysqli_fetch_assoc($this->result)))

{

mysqli_free_result($this->result);

return FALSE;

}

return $row;

}

 

// get insertion ID

public function getInsertID()

{

return mysqli_insert_id($this->link);

}

// count rows in result set

public function countRows()

{

if ($this->result !== NULL)

{

return mysqli_num_rows($this->result);

}

}

// implement destructor to close the database connection

function __destruct()

{

mysqli_close($this->link);

}

}

The above “MySQL class” does a decent job when it comes to performing queries, counting rows in result sets and finding insertion IDs. Though it isn't suitable for production environments, this sample class encapsulates enough functionality to perfectly fit the needs of a persistent class that will store its properties in a database table.

Thus, having at our disposal a class that allows you to abstract access to MySQL tables in a pretty respectable way, the only thing that remains undone is building the class that will persist through several HTTP requests.

But guess what? This class will be defined in the last section of this article. Therefore, please read the new few lines.



 
 
>>> 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 3 - Follow our Sitemap

Dev Shed Tutorial Topics: