Jump to content

Recommended Posts

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);
    }

}

Link to comment
https://forums.phpfreaks.com/topic/202905-simple-mysql-database-class/
Share on other sites

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.

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.

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.