My Database Snippet

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
MattSheppard
Forum Newbie
Posts: 4
Joined: Tue Oct 04, 2011 8:30 am

My Database Snippet

Post by MattSheppard »

Hi everybody,
I have a standard function that I have saved, that I put into just about all of my projects. I like to reuse my code and this (to me, at least seems) a standard piece of code to manage the killing of and connection to the MySQL database, but I'm not sure if there is a better way to do it. What do you all think? This is an annotated version I shared with a colleague.

Code: Select all

function Database($action = null){						# This function handles database connection and killing
	
		/*
			Usage guide:
				Before a query is fired:
					Database();
					
				After the query is fired:
					Database("kill");
			
			This will open the database connection, execute your script, then close the database connection.
		*/
		
		$server = "live";							# Variable can be "dev" or "live", change this to change your connection string
		switch ($server){
			case "live":
				$db["host"]		=	"localhost";	# Edit this to your host name
				$db["user"]		=	"root";			# Edit this to your MySQL username
				$db["pass"]		=	"";				# Edit this to your MySQL password
				$db["database"]	=	"db";			# Edit this to the name of your database
			break;
			case "dev":								# This is configured to work with a standard installation of WAMP Server, the layout is as above
				$db["host"]		=	"localhost";
				$db["user"]		=	"root";
				$db["pass"]		=	"";
				$db["database"]	=	"db";
			break;
		}
		
		$db["connection"] = mysql_connect($db["host"], $db["user"], $db["pass"]) or die ("Failed to connect to database server.");
		mysql_select_db($db["database"], $db["connection"]) or die ("Failed to connect to the database");
		
		if ( $action == "kill" ){
			mysql_close($db["connection"]) or die ("Failed to close the connection to the database.");	
		}
	}
Thanks in advance,
Matt

Change: My question was answered in one of the other forums, so I have made a change to this function
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: My Database Snippet

Post by VladSun »

Some things to say at first glance:
1) don't use die(), throw an exception;
2) don't use function arguments to change the behavior of a function so much - better use two functions, which leads to using a class with 2 methods;
3) don't use internal variables to configure the data connection (i.e. $server), better pass this to the constructor (assuming you use a class as said in 2) )
4) in fact, it's not a Database class/function, it's MySQL_Database class/function :) What will happen to your function if you need PostGreSql e.g. :)

PS: Even by having only the name of your function, it must be implemented as a class :) - it's a noun and there is no verb in it.
There are 10 types of people in this world, those who understand binary and those who don't
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: My Database Snippet

Post by Eric! »

I would also suggest you look into using PDO
Post Reply