Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 766685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:05:18+00:00 2026-05-14T17:05:18+00:00

I have a connection class for MySQL that looks like this: class MySQLConnect {

  • 0

I have a connection class for MySQL that looks like this:

class MySQLConnect
{
    private $connection;
    private static $instances = 0;

    function __construct()
    {
        if(MySQLConnect::$instances == 0)
        {
            //Connect to MySQL server
            $this->connection = mysql_connect(MySQLConfig::HOST, MySQLConfig::USER, MySQLConfig::PASS)
                or die("Error: Unable to connect to the MySQL Server.");
            MySQLConnect::$instances = 1;
        }
        else
        {
            $msg = "Close the existing instance of the MySQLConnector class.";
            die($msg);
        }
    }

    public function singleQuery($query, $databasename)
    {
        mysql_select_db(MySQLConfig::DB, $this->connection)
            or die("Error: Could not select database " . MySQLConfig::DB . " from the server.");
        $result = mysql_query($query) or die('Query failed.');
        return $result;
    }

    public function createResultSet($query, $databasename)
    {
        $rs = new MySQLResultSet($query, MySQLConfig::DB, $this->connection ) ;
        return $rs;
    }

    public function close()
    {
        MySQLConnect::$instances = 0;
        if(isset($this->connection) ) {
                mysql_close($this->connection) ;
                unset($this->connection) ;
        }
    }

    public function __destruct()
    {
        $this->close();
    }
}

The MySQLResultSet class looks like this:

class MySQLResultSet implements Iterator
{
    private $query;
    private $databasename;
    private $connection;
    private $result;

    private $currentRow;
    private $key = 0;
    private $valid;

    public function __construct($query, $databasename, $connection)
    {
        $this->query = $query;
        //Select the database
        $selectedDatabase = mysql_select_db($databasename, $connection)
            or die("Error: Could not select database " . $this->dbname . " from the server.");
        $this->result = mysql_query($this->query) or die('Query failed.');
        $this->rewind();
    }

    public function getResult()
    {
        return $this->result;
    }

//  public function getRow()
//  {
//      return mysql_fetch_row($this->result);
//  }

    public function getNumberRows()
    {
        return mysql_num_rows($this->result);
    }

    //current() returns the current row
    public function current()
    {
        return $this->currentRow;
    }

    //key() returns the current index
    public function key()
    {
        return $this->key;
    }

    //next() moves forward one index
    public function next()
    {
        if($this->currentRow = mysql_fetch_array($this->result) ) {
            $this->valid = true;
            $this->key++;
        }else{
            $this->valid = false;
        }
    }

    //rewind() moves to the starting index
    public function rewind()
    {
        $this->key = 0;
        if(mysql_num_rows($this->result) > 0) 
        {
            if(mysql_data_seek($this->result, 0) ) 
            {
                $this->valid = true;
                $this->key = 0;
                $this->currentRow = mysql_fetch_array($this->result);
            }
        }
        else
        {
            $this->valid = false;
        }
    }

    //valid returns 1 if the current position is a valid array index
    //and 0 if it is not valid
    public function valid()
    {
        return $this->valid;
    }
}

The following class is an example of how I am accessing the database:

class ImageCount
{
    public function getCount()
    {
        $mysqlConnector = new MySQLConnect();
        $query = "SELECT * FROM images;";
        $resultSet = $mysqlConnector->createResultSet($query, MySQLConfig::DB);
        $mysqlConnector->close();
        return $resultSet->getNumberRows();
    }
}

I use the ImageCount class like this:

if(!ImageCount::getCount())
{
    //Do something
}

Question: Is this an okay way to access the database? Could anybody recommend an alternative method if it is bad?

Thank-you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:05:18+00:00Added an answer on May 14, 2026 at 5:05 pm

    Hey Mike, there’s nothing wrong with implementing your own classes to handle database connection, what you have so far is fine, however PHP already provides an interface for handling DB connections regardless of the database manager you are connecting to. I’d recommend you to take a look at it http://www.php.net/manual/en/book.pdo.php since it has mostly all the functionality needed for handling queries, statements, resultsets, errors, and so forth.

    Cheers,
    M.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that listens for a connection on a socket: public class
This is my code that is meant to connect to a MySQL server and
You can have different naming convention for class members, static objects, global objects, and
I have a collection of classes that inherit from an abstract class I created.
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a connection string being passed to a function, and I need to
Per this helpful article I have confirmed I have a connection pool leak in
I have three files: one called sql.php witch has a class db that I
I have a java program that connects to a MySql database and it's working
I have a function that returns the result of a query. The input arguments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.