My First PHP Class
Posted: Fri Jul 18, 2008 10:56 am
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I'm having a go at moving to OO PHP and here is my first class. It seems to work but I was wondering what others thought. It's designed to handle connections, queries and disconnections to a MySQL database. I'm using a custom error handler for failures. How can I improve it?
And here is how I use it to connect:
Many thanks in advance for any feedback or help!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I'm having a go at moving to OO PHP and here is my first class. It seems to work but I was wondering what others thought. It's designed to handle connections, queries and disconnections to a MySQL database. I'm using a custom error handler for failures. How can I improve it?
Code: Select all
<?php
/**
* DatabaseHandler.php
* Class for database operations.
* Sevices include: database connection, disconnection, database queries and free result calls.
*/
class DatabaseHandler {
/*
* Declare some variables
*/
private $site;
private $dbHost;
private $dbUser;
private $dbPass;
private $dbName;
private $connection;
private $dbSelected;
private $query;
private $free;
private $disconnect;
/*
* Makes a connection to the database server.
* Then selects the database.
* Database connection parameters are located in db_config.php.
* Returns the database connection handle on success.
*/
public function db_connect($site) {
require($site . "/db_config.php");
$connection = @mysql_connect($dbHost, $dbUser, $dbPass);
if(!$connection) {
trigger_error("Could not connect to database server.", E_USER_ERROR);
} else {
$dbSelected = @mysql_select_db($dbName, $connection);
if(!$dbSelected) {
trigger_error("Could not select database - " . $dbName, E_USER_ERROR);
} else {
return $connection;
}
}
}
/*
* Issues queries to the database.
* Returns the result set on success.
*/
public function query($query) {
$result = mysql_query($query);
if(!$result) {
trigger_error("Could not issue query.", E_USER_ERROR);
} else {
return $result;
}
}
/*
* Free result.
* Returns true on success.
*/
public function free_result($connection) {
$free = @mysql_free_result($connection);
if(!$free) {
trigger_error("Could not free result set.", E_USER_ERROR);
} else {
return $free;
}
}
/*
* Disconnects from the database server.
* Returns true on success.
*/
public function db_disconnect($connection) {
$disconnect = @mysql_close($connection);
if(!$disconnect) {
trigger_error("Could not close database connection.", E_USER_ERROR);
} else {
return $disconnect;
}
}
} // end class
?>Code: Select all
<?php
include("includes.php");
$dbConn = new DatabaseHandler;
// $site is defined in includes.php
$connection = $dbConn->db_connect($site);
$query = "SELECT * FROM test";
$result = $dbConn->query($query);
while($row = @mysql_fetch_row($result)) {
$data = $row[0];
echo "<p><strong>" . $data . "</strong></p>";
}
$free_result = $dbConn->free_result($result);
$disconn = $dbConn->db_disconnect($connection);
?>~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: