RE: [PHP] Have I over done this?

From: Date: Mon, 18 Aug 2003 16:52:57 +0000
Subject: RE: [PHP] Have I over done this?
Groups: php.general 
Request: Send a blank email to php-general+get-159933@lists.php.net to get a copy of this message
John Taylor-Johnston <mailto:taylorjo@collegesherbrooke.qc.ca> on Saturday, August 16, 2003 5:19 PM said: > Have I over done this? That I don't know. > Can I clean up this code any? Yes. 1. You can take your open/closeserver() functions one step farther and create a query function (can be called something like dbQuery()). Anytime you repeat code you should look into a way of turning that repeating code into a function. For example you seem to follow this: OpenServer(); $sql = "SELECT mystuff"; mysql_query($sql) or die(..); CloseServer(); Turn that into a function instead and save yourself the hassle of typing mysql_query(..) or die(..); Function myQuery($sql) { OpenServer(); mysql_query($sql) or die(..); CloseServer(); } Now all you have to do is this: $sql = "SELECT mystuff"; myQuery($sql); 2. Only because it's redundant, you shouldn't Open/CloseServer() in each code block. Instead, OpenServer() and the beginning of the page and CloseServer() at the end of the page. <? OpenServer(); // entire page content goes here CloseServer(); ?> That would turn the function I wrote above into this: Function myQuery($sql) { mysql_query($sql) or die(..); } 3. In my limited experience with php I would recommend you create a database class that will organize all your db functions into one object. Shoot me an email offlist if you'd like to see what I've written. HTH, Chris.

« previous php.general (#159933) next »