Jump to content

Is this a good way of connecting to a database?


papaface

Recommended Posts

Hello,

Can someone tell me if this is a good, and efficient way of connecting to a mysql database?

The page is connect.php and would be included on all php files that require a database.

Is this a good way, or is there a better way of doing it?

<?php
class DatabaseConnect
{
protected $dbname = "papaface_qss";
protected $dbusername = "papaface_default";
protected $dbpassword = "***";
protected $hostname = "localhost";
public $connection;

function __construct()
	{
	$this->connection = mysql_connect($this->hostname,$this->dbusername,$this->dbpassword) or die (mysql_error());
	mysql_select_db($this->dbname,$this->connection) or die ("Could not select the database: " . mysql_error());
	//echo "Connected to the database.";
	}
}
$_conn = new DatabaseConnect();
?>

Thanks, any input would be great :D

Link to comment
Share on other sites

This is the way I connect. There is only one more feature your connection could have to make it better, a query($sql) method that returns either the entire results, true, or false (depending on the type of query, etc etc). I'm sure that others will have different ways of connecting.

Link to comment
Share on other sites

I agree with thorpe.  I would probably just do it functionally and return a connection pointer.

 

If you intend to add more functions, you still don't need to make a class; you could simply just have a bunch of functions in connect.php file to help you manage and mess with your connection.

 

Otherwise, there's really no problem with how you're doing it.  In fact, if you define the __destruct() method to close your connection automagically, then this may actually be better!

(ie: Add the following function to your class:)

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

Link to comment
Share on other sites

In fact, if you define the __destruct() method to close your connection automagically, then this may actually be better!

(ie: Add the following function to your class:)

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

 

Database connection will be closed by PHP automatically at the end of script execution. There is no need to do it yourself. Also, you shouldn't close important things in destructors (file handles, database connections, sockets etc.) as you cannot control the order of which objects are destroyed.

 

You should only close a database connection if you need it to end before the script ends. Otherwise there is no point in doing it.

Link to comment
Share on other sites

@ Daniel:  Didn't know that!  There are some nice things about PHP :P!  My background is in desktop programming languages, especially strict, strongly typed ones like Java and C#, and in those cases, closing your connections is an iron rule of sorts.  (Not as bad as deallocating memory in C, but still pretty strict.)  So, I'm just very used to closing things when I'm done with it and making sure not to use it again.  (I suppose I might get a very slight performance increase for it though?)  In any case, PHP is very different in a lot of ways, so it's been throwing me for a loop :).

Link to comment
Share on other sites

I suppose I might get a very slight performance increase for [closing resources before the script ends] though?

 

Not really. PHP detects when a resource is not used anymore frees it automatically. It's just a matter of who that does it: you or PHP's garbage collector.

Link to comment
Share on other sites

  • 1 month later...

My own class that I wrote has a query() function that executes SQL and stores the result internally. I did this to add a bit of abstraction - I also have a row() function that returns the next row from the stored result, so I can do something like this in my code:

while ( ($row=$db->row()) !== false ) { /* do the shiz here */ }

Link to comment
Share on other sites

  • 2 weeks later...

Hi!

 

I'm really new to this but do have a question that is not to silly.

 

In the first classes could he not set the values for the connection but make functions that assign the connection information?

 

 

Stephen

 

Can you explain what you mean exactly?

Link to comment
Share on other sites

I think stephen means something like this:

 

 

<?php

class DatabaseConnect
{
protected $dbname;
protected $dbusername;
protected $dbpassword;
protected $hostname;
public $connection;

function __construct($hostname,$username,$password,$database)
	{
                $this->hostname = $hostname;
                $this->dbusername = $username;
                $this->dbpassword = $password;
                $this->dbname = $database;

	$this->connection = mysql_connect($this->hostname,$this->dbusername,$this->dbpassword) or die (mysql_error());
	mysql_select_db($this->dbname,$this->connection) or die ("Could not select the database: " . mysql_error());
	//echo "Connected to the database.";
	}
}
$_conn = new DatabaseConnect("localhost", "papaface_default", "****", "papaface_qss");

?>

 

This way he could create more than 1 DatabaseConnect for different MySQL connections.

e.g.

<?php
$_conn1 = new DatabaseConnect("localhost", "papaface_default", "****", "papaface_qss");
$_conn2 = new DatabaseConnect("123.456.789.12", "other_user", "some_password", "other_database");
?>

Link to comment
Share on other sites

As I said, it's just a naming convention used by some people, nothing else. In PHP4 the following would work perfectly fine, but people might get confused:

<?php
class Test
{
    var $_variable;
    
    // some methods
}

$obj = new Test();
$obj->_variable = 'Test';
echo $obj->_variable;
?>

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.