2

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');
        }
    }
}
user3684526
  • 103
  • 8
  • Would it be a good idea to do global $database; and use the $database from the index.php? If so would I have to do the connect function again? Or would the one from index.php connect in other classes? – user3684526 May 28 '14 at 16:43
  • Try to avoid global variables as much as possible. – Jeroen May 28 '14 at 16:45
  • Go for a singleton approach. http://www.ricocheting.com/code/php/mysql-database-class-wrapper-v3 – fortune May 28 '14 at 16:52

2 Answers2

2

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.

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • is PDO just mysql_pconnect Sorry if im being dull. – user3684526 May 28 '14 at 17:02
  • No, PDO is a totally different API. It's an object-oriented extension in PHP for database access. PDO supports a bunch of databases, not just MySQL. Read about it here: http://php.net/pdo – Bill Karwin May 28 '14 at 17:11
  • Is this already with php or do i need to download it? And does mysqli need to be downloaded? Im thinking of making a multiple class to choose beetween multiple database types (pdo,mysqli) – user3684526 May 28 '14 at 17:18
  • I did that when I worked on the Zend Framework in 2007. Given the choice, I'd rather just use PDO. PDO has been included since PHP 5.1. Mysqli has been included since PHP 5.0. – Bill Karwin May 28 '14 at 17:23
0

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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77