Connecting to a database I'm working on a web application that I inherited from a colleague long gone. To connect to the MySQL database I use the following classes:

statement.php

&lt;?php
//class sql
//{
    class Statement
    {
        private $m_connection;

        function __construct($connection)
        {
            $this-&gt;m_connection = $connection;
        }

        function query($query)
        {
            $connection = $this-&gt;m_connection;
            $handle = $connection-&gt;getHandle();

            $result = mysql_query($query,$handle);

            if ($result === FALSE)
                throw new Exception(mysql_error());

            $instance = new Result($this);
            $instance-&gt;setHandle($result);

            return $instance;
        }
    }
//}
?&gt;


result.php

&lt;?php
//class sql
//{
    class Result
    {
        private $m_handle;
        private $m_statement;

        function __construct($statement)
        {
            $this-&gt;m_statement = $statement;
        }

        function setHandle($handle)
        {   
            $this-&gt;m_handle = $handle;
        }

        function fetch()
        {
            $handle = $this-&gt;m_handle;          
            $row = mysql_fetch_array($handle);

            return $row;
        }
    }
//}
?&gt;


connection.php

&lt;?php
include_once("statement.php");
include_once("result.php");

//class sql
//{
    class Connection
    {
        private $m_handle;

        function __construct($server,$username,$password)
        {
            $handle = mysql_connect($server,$username,$password);
            $this-&gt;m_handle = $handle;
        }

        function __destruct()
        {
            $handle = $this-&gt;m_handle;
            @mysql_close($handle);
        }

        function createStatement()
        {
            return new Statement($this);
        }

        function getHandle()
        {
            return $this-&gt;m_handle;
        }
    }
//}
?&gt;


named.php

&lt;?php
include_once("connection.php");

function createNamedConnection($name)
{
    if ($name == "dbalias1")
    {
        $connection = new Connection("first.host.com","user","uspw");

        $statement = $connection-&gt;createStatement();
        $statement-&gt;query("USE db1");

        return $connection;
    }   
    if ($name == "dbalias2")
    {
        $connection = new Connection("second.host.com","user2","nouse");

        $statement = $connection-&gt;createStatement();
        $statement-&gt;query("USE db2");

        return $connection;
    }

    return null;
}
?&gt;


To have a connection I can use the following script:

$connection = createNamedConnection("dbalias1");
$statement = $connection-&gt;createStatement();
$query = "SELECT   *
          FROM     tContent c
          WHERE    c.cID &gt; 100";
$result = $statement-&gt;query($query);
while(($row = $result-&gt;fetch()) !== FALSE){
    //do something
}


The problem is that those classes caused me a lot of trouble, and I'm sure there is work to be done on them. I don't know where to begin to make those classes more secure and easy to use.
Consider using mysqli instead of mysql:

mysqli

Mysqli (the "i" stands for "improved") is more OO-friendly and has better security.  It also seems like the standard mysql functions are being deprecated (some of them anyway).