TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-19-2008, 11:16 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default 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?
Sam Granger is offline  
Reply With Quote
Old 01-19-2008, 11:47 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-24-2008, 04:57 PM   #3 (permalink)
The Wanderer
 
cherries's Avatar
 
Join Date: Oct 2007
Posts: 20
Thanks: 0
cherries is an unknown quantity at this point
Default

Looks great but you wasted your time, mysqli
cherries is offline  
Reply With Quote
Old 01-24-2008, 05:52 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-24-2008, 06:35 PM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-24-2008, 11:07 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Wouldn't it be better to call the Variables: Properties, and the Functions: Methods, considering that's what they are called in encapsulation? :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design