Jump to content

Is this right?


DyslexicDog

Recommended Posts

I pretty new to PHP and OOP, but I managed to get a working object setup. I was wondering if someone could make comments on my code and let me know if I'm doing this the right way.

 

<?php
class dbtools{
//This class will connect to a defined database when called
//then allow manipulation of the database.
	private $parameters = array
				(
					'username' => 'user',
    						'password' => 'pass',
    						'database' => 'data',
    						'server'   => 'localhost'
    					);
	private $query  = null;
	private $result = null;
	private $rows	= null;
    function __construct()
    { //after loading default settings for database connection connect to server and 
      // attach to database.
	@mysql_connect($this->parameters['server'] , 
		$this->parameters['username'],$this->parameters['password']) or 
		die( "unable to connect to database");
    	@mysql_select_db($this->parameters['database']) or die( "Unable to select database");
}

    public function setQuery($query)
{
	$this->query = $query;
}

    public function dbquery()
    { //this function will return a raw mysql result to be formatted externaly
    	$this->result=mysql_query ($this->query) or 
    		die( "Unable to query database<br>".$query."<br>");
    	return $this->result;		
}
public function rowCount()
{
	$this->rows = mysql_num_rows($this->result);
	return $this->rows;		
}
public function dbescape($escape)
//Use this function to escape any possible SQL injection attacks
//this function tests for magic quotes and removes them if it is active
//then passes the incoming data to the real escape string function.
{
	if ( get_magic_quotes_gpc())
	{
		$escape = stripslashes($escape);
	}

	$escape = mysql_real_escape_string($escape) or 
    		die("Unable to add escape characters <br>".$escape."<br>");
    	return $escape;	
}

Function __destruct()
{
	mysql_close(); 	//close mysql connection.
}
};
?>

Link to comment
Share on other sites

Yes, it is properly used :).  You might want to create the ability to use a different set of database parameters though.  As it is right now you would have to modify the class directly to connect to the proper database.  Just a thought :).

Link to comment
Share on other sites

You might want to create the ability to use a different set of database parameters though.  As it is right now you would have to modify the class directly to connect to the proper database.  Just a thought :).

 

Like making the the variables public, or creating a method to update the variables?

Link to comment
Share on other sites

Try this:

 

    function __construct($user = 'user, $password='pass', $database = 'data', $server = 'localhost')

    { //after loading default settings for database connection connect to server and

      // attach to database.

@mysql_connect($server ,

$user,$pass) or

die( "unable to connect to database");

    @mysql_select_db($database) or die( "Unable to select database");

 

The whole $parameters array is redundant. You are using it to store default variables, but adding default values to the constructor works better.

You can't do this by making the array public, because when you initialize the class, it is already connected.

 

i.e.:

 

$db = new dbtools(); // The database is already connected. Changing the array does nothing now

$db->parameters['username'] = "New User"

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.