sw45acp Posted May 26, 2010 Share Posted May 26, 2010 Hi, I am looking for a way to shorten the amount of code that I have to write when only querying and retrieving from a database, so I have created this very simple class to do so. I've looked at several other classes for this, however they are all to complex. I would like your opinion on what I have written, and if it can be revised/rewritten I am all ears and open for suggestions/criticism. Thank you. class queryHelper { function __construct() {} /** * Simply executes a query * @param q: A query string * @param type: 0 = Just the query, nothing more * 1 = Return mysql_fetch_array * 2 = Return mysql_num_rows */ public function query($q,$type = 0) { $query_id = mysql_query($q); if (!$query_id) { die('The following query was attempted: '.$q.'<br />Error: '.mysql_error()); } switch($type) { case 0: return $query_id; break; case 1: return $this->fetch($query_id); break; case 2: return $this->numRows($query_id); break; } } /** * Returns a mysql_fetch_array result. */ public function fetch($q_id) { $recordset = array(); while($rows = mysql_fetch_array($q_id)) { $recordset[] = $rows; } return $recordset; } /** * Returns a mysql_num_rows result. */ public function numRows($q_id) { return mysql_num_rows($q_id); } } Quote Link to comment https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/ Share on other sites More sharing options...
mrMarcus Posted May 26, 2010 Share Posted May 26, 2010 refrain from using die() to handle errors as users don't care about your database errors, and don't overly appreciate receiving a blank page with: The following query was attempted: Query goes here. Error: Error message goes here. the above means nothing and should at best be used in development and NOT production. instead, create an error message/display function within your class to nicely redirect your user to a more helpful page upon any errors your code throws at them. perhaps something similar to a 404 page of sorts where the user is offered direction and links to areas of your site. But be sure and let them know an error occurred otherwise they might be pissed that they are being redirected with no known reason. And instead of display the query and mysql error, simply log it for your eyes only. Quote Link to comment https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/#findComment-1063334 Share on other sites More sharing options...
sw45acp Posted May 26, 2010 Author Share Posted May 26, 2010 Yes I will make sure to remove that from anything that is not in development. I will probably keep two classes, one for development and one for the real deal. Quote Link to comment https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/#findComment-1063336 Share on other sites More sharing options...
mrMarcus Posted May 26, 2010 Share Posted May 26, 2010 Yes I will make sure to remove that from anything that is not in development. I will probably keep two classes, one for development and one for the real deal. i would recommend against this. while it might sound like a good idea, you are bound to omit code from one or the other, ultimately confusing yourself as your classes get larger and larger. keep one class with proper error handling function that logs your errors to a file. voila, no reason for multiple classes. Quote Link to comment https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/#findComment-1063339 Share on other sites More sharing options...
sw45acp Posted May 26, 2010 Author Share Posted May 26, 2010 Ok that's good advice. Keeping two files updated and having to switch back and forth between them is a lot of needless work. Quote Link to comment https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/#findComment-1063343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.