Click to See Complete Forum and Search --> : My First Class (MySQL)


Popcorn
05-21-2007, 06:31 PM
Using PHP5

<?php

/*
Author : Mathew Davies
Creation : Friday, May 19th 2006
Contact : leveldesign.info@gmail.com
*/

class MySQL {

var $server;
var $username;
var $password;
var $database;
var $connect;

function connect($server = NULL, $username = NULL, $password = NULL) {
if ($server == NULL || $username == NULL || $password == NULL) {
$server = $this->server;
$username = $this->username;
$password = $this->password;
}

$this->connect = @mysql_connect($server, $username, $password);
if (!$this->connect) {
$this->error();
} else {
echo "Connected To MySQL <br>";
}
}

function database($database = NULL) {
if ($database == NULL) {
$database = $this->database;
}

$database = @mysql_select_db($database);
if(!$database) {
$this->error();
} else {
echo "Connected To The Database <br>";
}

}

function error() {
echo "Error Code : " . mysql_errno() . " <br> Error Message : " . mysql_error();
if ($this->connect) mysql_close($this->connect);
exit;
}

}

?>


It works pretty well and you can pass the connection details through the function directly or set the variables.

Crits welcome.

Ps : Not finished. just making sure I'm heading the right direction.

NogDog
05-27-2007, 05:32 PM
Couple things I'd change:

1. In general, any methods that might be called externally should return something. In the case of your connect() and database() methods, they should probably return FALSE if they fail and TRUE if they are successful, allowing the calling code to implement a logical branch depending on the success or failure of the call.

2. I would not have your error() method automatically echo the error message, as this could be a security issue on a live system. Saving the error to a class variable should be sufficient. Then, along with the above comment, if a method returns false, you can access the object's error variable if desired or not, depending on the current state of development of the script utilizing this class (or even provide a method for getting the last error message or such).

Popcorn
05-28-2007, 12:10 PM
Thanks for the suggestions NogDog, much appreciated. I've been working on it and here is an updated version.

<?php

/*
Author : Mathew Davies
Creation : Friday, May 19th 2006
Contact : leveldesign.info@gmail.com

Example Usage :

# // Include the file into your application.
# include('mysql.conf.php');
#
# // Initate a new MySQL class.
# $mysql = new MySQL();
#
# // Lets set the MySQL server parameters.
# $mysql->setServer ('localhost'); // MySQL server address.
# $mysql->setUsername ('root'); // Username used to login to MySQL.
# $mysql->setPassword ('admin'); // Password used to login to MySQL
# $mysql->setDatabase ('levelde_core'); // The database you want to work with.
#
# // Connect to the MySQL with the parameters set as above.
# $mysq->connect();
#
# // Alternative way of connecting to the MySQL server.
# $mysql->connect('server'.'username'.'password');
#
# // Select the database ready for use.
# $mysql->database();
#
# // Alternative way of selecting the database
# $mysql->database('database');

The MySQL is now ready to use, below you will find more information on the different functions
this class has to offer.

// This will execute the query inbetween the brackets. Protection against SQL injections is already done
// within the function.
# $mysql->query('SELECT * FROM examples');

// Fetches data from the last used query. MySQL data is fetched using the mysql_fetch_array(); function.
// Here is the correct way to use the function

# $levelde_news = $mysql->query('SELECT * FROM levelde_news');
#
# while ($row = $mysql->fetch($levelde_news)) {
# $result[] = $row;
# $smarty->assign('news', $result);
# }

*/

class MySQL {

private $server;
private $username;
private $password;
private $database;
private $connection_handle;
private $database_handle;
private $query;
private $result;

function setServer ($server) {
$this->server = $server;
}
function setUsername($username) {
$this->username = $username;
}
function setPassword($password) {
$this->password = $password;
}
function setDatabase($database) {
$this->database = $database;
}
function connect ($server = NULL, $username = NULL, $password = NULL) {
if(!$server == NULL && !$username = NULL && !$password == NULL) {
$this->server = $server;
$this->username = $username;
$this->password = $password;
}
$this->connection_handle = @mysql_connect($this->server, $this->username, $this->password);
if (!$this->connection_handle) {
$this->error();
}
}
function database ($database = NULL) {
if (!$database == NULL) {
$this->database = $database;
}
$this->database_handle = @mysql_select_db($this->database, $this->connection_handle);
if (!$this->database_handle) {
$this->error();
}
}
function query ($query = NULL) {
if (!$query == NULL) {
$this->query = $query;
}
$this->result = mysql_query($this->query, $this->connection_handle);
}
function fetch ($result = NULL) {
if(!$result == NULL) {
$this->result = $result;
}
$row = mysql_fetch_array($this->result);
return $row;
}
function error () {
die ("Error Code : " . mysql_errno() . " <br> Error Message : " . mysql_error());
if ($this->connection_handle) {
mysql_close($this->connection_handle);
}
}
function close () {
if ($this->connection_handle) mysql_close($this->connection_handle);
}

}

?>


I shall implement your ideas.

Kind Regards,
-Matt

Piranha
05-28-2007, 12:20 PM
2. I would not have your error() method automatically echo the error message, as this could be a security issue on a live system. Saving the error to a class variable should be sufficient. Then, along with the above comment, if a method returns false, you can access the object's error variable if desired or not, depending on the current state of development of the script utilizing this class (or even provide a method for getting the last error message or such).

I don't agree about this. Having an error method allow you to develop using the error method to get all errors. When everything is ready for the production server it is easy to use a database class where the row that outputs the error is removed. In other words, you have one place to edit (and only need to do it once) to get errors printed or not. If you handle errors in the programming code it is harder, and you might miss one place.

NogDog
05-29-2007, 12:16 AM
Actually, I normally include a $debugFlag in such a class that defaults to false, with an optional parameter to the constructor which defaults to false. If instantiated as true, then my error method will output errors via the user_error function, otherwise it will log them via error_log. So when I go "live" all I have to do is remove the last arg in the instantiation. In either case, the error is still also saved as a class variable that my script can access if I want to output it to the user regardless of the debug setting .