user3684526
user3684526

Reputation: 103

PHP Database Advice

I'm sorry for the lack of information on this question I just need some advice on some code. I think only a programmer can answer this because my code is unique and I couldn't find any answers that help me.

Index.php

<?php
include ( 'Blue/BlueDatabase.php' );
include ( 'Blue/BlueUsers.php' );

use Blue\BlueDatabase;
use Blue\BlueLoader;

$database = new BlueDatabase();
$database->Connect('localhost', 'root', '', 'database');
?>

And now I have my "BlueLoader" class and I have this code in the class aswell

$database = new BlueDatabase();
$database->Connect('localhost', 'root', '', 'database');

What I want to know is. Is it worth adding that code to every class? It makes it look untidy and I think there will be a more stabler way of doing it. Do i need to do the connect function once ive done it before? does it need to be done for every class? I'm just new to php and unsure about some things. Is this script stable? / Secure

Any advice or answers will be helpful

Just incase you need my connect function (All my mysql commands are in an array like

//Example:
$commands['mysql_query'] = mysql_query;

final public function connect($sHost, $sUser, $sPass, $sName, $sPort = false, $sType = 'mysql_connect')
{
    if(!$this->connected)
    {
        if ($sPort)
        {
            $sHost = $sHost . ':' . $sPort;
        }
        $this->connection = $this->_aCmd[$sType]($sHost, $sUser, $sPass);

        if($this->connection)
        {
            $mydatabase = $this->_aCmd['mysql_select_db']($sName, $this->connection);

            if($mydatabase)
            {
                $this->connected = true;
            }
            else
            {
                die('could not connect to database');
            }
        }
        else
        {
            die('could not connect to host');
        }
    }
}

Upvotes: 2

Views: 93

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562330

Advice:

  • Stop using the deprecated ext/mysql extension. Don't bother trying to wrap it in an OO framework.

  • Use PDO, because it already has a good OO usage, and has several other features that ext/mysql doesn't have. Mysqli is a runner-up, but I find PDO is easier to use.

  • Don't open a new connection in each class that uses a database. Create one connection and pass the connection object to each such class, probably as a constructor argument.

  • An alternative is to use a Registry class to store "global" objects like database connections.

Upvotes: 2

Sammitch
Sammitch

Reputation: 32232

Globals are great! They are perfect for indicating that you're taking a sub-optimal approach and you need to step back and re-evaluate.

Here are a few different ways you can address the issue.

$dbh = new DatabaseObject($connection_info);

$foo = new Object1($dbh);

$bar = new Object2();
$bar->GiveDbh($dbh);

$baz = new RegistryObject();
$baz->register('dbh', $dbh);
$bom = new Object3($baz);

I'd also include a Singleton method, but SO yells at me every time I do. :/

Also, you should probably re-jigger your database class to stop using mysql_* functions. Yadda yadda PDO MySQLi, you know the drill.

Upvotes: 0

Related Questions