Hi! Im making a database class in PHP. And i thought i might post it here, and ask for your opinions and how i can do this better? :)

<?php

    class Database {

        var $db_host = 'localhost';         // Server
        var $db_user = 'root';              // Username
        var $db_pass = '';                  // Password
        var $db_name = 'darqnet';           // Database
        var $db_char = 'utf-8';             // Charset
        var $db_link;                       // Connection is stored here
        var $db_rslt;                       // Query result
        var $db_numr;                       // Numrows of query


        function __construct() {

            $this -> db_link = mysql_connect(
                $this -> db_host, 
                $this -> db_user, 
                $this -> db_pass
            ) or die('Kunne ikke koble til MySQL');

            mysql_select_db(
                $this -> db_name,
                $this -> db_link
            ) or die('Kunne ikke åpne database ' . mysql_error());

            mysql_set_charset(
                $this -> db_char,
                $this -> db_link
            );

        }


        function query($sql) {

            $statement = explode(' ', $sql);
            switch ($statement[0]) {

                case ($statement[0] == 'SELECT') :
                    $this -> db_rslt = mysql_query($sql, $this -> db_link);
                    $this -> db_numr = mysql_num_rows($this -> db_rslt);
                break;

                case ($statement[0] == 'UPDATE' || $statement[0] == 'INSERT') :
                    $this -> db_rslt = mysql_query($sql, $this -> db_link);
                break;

            }

        }


        function num_rows() {

            return $this -> db_numr;

        }


        function rows() {

            $rows = array();

            for ($x = 0; $x < $this -> db_numr; $x++) {
                $rows[] = mysql_fetch_assoc($this -> db_rslt);
            }

            return $rows;

        }

    }

?>
Member Avatar for diafol

NB. I am still an OOP NOOB and may always be so. So pinch of salt with the following...

First thing that struck me was that you're using mysql_*. This is about to go legs up, so mysqli_* or PDO would be a better bet. Also, set as many props and methods to private or at least protected as possible. They're all public at the moment, so the props can be overwritten. Also, it seems to only take UPDATE and SELECT. This is a difficult way of recognising which one to use as often there may be a slightly different syntax, e.g. '(SELECT...' for UNION queries etc. DELETE and INSERT should be included too if you're making a generic class. Whether or not you use db connection details hard-coded into the class or pass them when the object is created I suppose is up to you. Passing them at runtime (to be used in __construct) may allow you a greater degree of flexibility.

Another thing to ponder is whether this needs to be a concrete object or whether you want to create an 'singleton' class, so that other classes use the one instance of it. I recently started using my own PDO wrapper as such and found it a lot easier to manage (one instance across many classes).

Before you all start calling me out on the 'singleton' name - yeah I know, singleton's a 'pattern'.

My opinion: making your own database class might be worth only if you are doing it in order to learn about database handling and OOP. If you need a good and verstaile db class there are many arround which have been developed by dedicated people/teams and through considerable amount of time spent. I use Pear MDB2 class which has thousands of lines of code and works very well (OK, it still has some shortcommings). It is good to have a look at the source code there and you will find many good ideas like transactions, prepared statements, quoting, escaping etc. It abstracts database functions so they are usable for many different databases (mysql/mysqli, mssql, oracle ...).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.