TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Show Off (http://www.talkphp.com/show-off/)
-   -   Please review my MySQL Class. (http://www.talkphp.com/show-off/2019-please-review-my-mysql-class.html)

Sam Granger 01-19-2008 11:16 PM

Please review my MySQL Class.
 
Hello!

My PHP isn't fantastic and usually I get others to program the main classes for me. However I decided to try and make a class myself in PHP5 - some help was given over at a different community because I forgot to define some variables.

Anyway, my class:

Code:

<?php

/*
 @author Sam Granger
 @copyright 2008 Intrellia. All Rights Reserved.
*/

class DBConnection {
       
        var $connection;
        var $query;

        public function __construct($db_server, $db_username, $db_password, $db_database) {
                $this->connection = mysql_pconnect($db_server, $db_username, $db_password);
                mysql_select_db($db_database, $this->connection);
        }

        function __destruct() {
                @mysql_close($this->connection);
        }

        public function DBQuery($sql) {
                $this->query = mysql_query($sql) or die(mysql_error());
                return $this->query;
        }

        public function DBRow() {
                $row = mysql_fetch_row($this->query);
                return $row;
        }

        public function DBArray() {
                $array = mysql_fetch_array($this->query);
                return $array;
        }

        public function DBCount() {
                $num = mysql_num_rows($this->query);
                return $num;
        }

}

?>

Please critisize. What could I do better?

Alan @ CIT 01-19-2008 11:47 PM

Hi Sam,

Looking good so far :-)

A couple of suggestions though, since you're using public/private keywords for the functions it wouldn't hurt to use them for the variables as well.

PHP Code:

protected $connection NULL;
protected 
$query NULL

Also, might be worth putting some error checking in there. Since you're doing it in PHP5, might as well use exceptions for it:

PHP Code:

public function DBQuery($sql)
{
    if (!
$this->query mysql_query($sql))
    {
        throw new 
Exception('Query error: ' mysql_error());
    }

    return 
$this->query;


Then in your PHP you could use something like this to check for the error:

PHP Code:

try
{
    
$db->query("SELECT something FROM somewhere");
}
catch (
Exception $e)
{
    echo 
'Oops, an error occured! - Error returned was: ' $e->getMessage();


Just makes for neater errors than plain old mysql_errors(), particularly when you are outputting the error to the visitor.

Other than that, looks good :-)

Alan

cherries 01-24-2008 04:57 PM

Looks great but you wasted your time, mysqli

Alan @ CIT 01-24-2008 05:52 PM

Self-education is never a waste of time :-)

Also, rather than use a specific extension like mysqli, you'd be better of using PDO.

Alan

RobertK 01-24-2008 06:35 PM

I agree with Alan, but for an alternate reason as well. My latest server move has put me on a server without BOTH PDO and mysqli. Never bind yourself to modules that may not be there when you can write your own easily enough.

Orc 01-24-2008 11:07 PM

Wouldn't it be better to call the Variables: Properties, and the Functions: Methods, considering that's what they are called in encapsulation? :P


All times are GMT. The time now is 11:05 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0