Re: MySQL class. Thoughts?

From: Date: Wed, 21 Jan 2009 23:45:34 +0000
Subject: Re: MySQL class. Thoughts?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-286973@lists.php.net to get a copy of this message
Jay Moore wrote:
This is a MySQL class I use and I wanted to get everyone's thoughts on how/if I can improve it. This is for MySQL only. I don't need to make it compatible with other databases. I'm curious what you all think. Thanks, Jay Class: ------ <?php // Standard MySQL class class do_mysql {
    // Constructor
    function __construct()
    {
        $this->do_mysql();
    }
        // Destructor
    function __destruct()
    {
        //$this->close();
    }
        function do_mysql()
    {
        $this->login = '';
        $this->pass = '';
               $this->link = @mysql_connect('localhost', $this->login, $this->pass) or die('Could not connect to the database.');
    } // End do_mysql
    // Functions       function close()
    {
        if ($this->link)
        {
            mysql_close($this->link);
            unset($this->link);
        }
    } // End close
        function fetch_array()
    {
        return mysql_fetch_array($this->result);
    } // End fetch_array
        function last_id()
    {
        return mysql_insert_id($this->link);
    } // End last_id
           function num_rows()
    {
        return mysql_num_rows($this->result);
    } // End num_rows
        function process($database = '')
    {
        if (is_null($this->query))
        {
            die('Error:  Query string empty.  Cannot proceed.');
        }
                   $this->db = @mysql_select_db($database, $this->link) or die("Database Error:  Couldn't select $database <br />" . mysql_error());
        $this->result = @mysql_query($this->query, $this->link) or die('Database Error:  Couldn\'t query. <br />' . mysql_error() . "<br /><br /> $this->query");
    } // End process
        function sanitize(&$ref)
    {
        $ref = mysql_real_escape_string($ref);
    } // End sanitize
    } // End do_mysql
?> Sample usage: $value = 'value'; $sql = new do_mysql(); $sql->sanitize($value); $sql->query = "SELECT * FROM wherever WHERE field = '$value'"; $sql->process('dbname'); $sql->close(); if ($sql->num_rows()) {
    while ($row = $sql->fetch_array())
    {
        do stuff;
    }
}
I think I'd use one of 20+ Pear classes. They are pretty throughly tested because of the thousands of users.

« previous php.general (#286973) next »