2

Ok so I just started taking a 3rd year module on advanced web design focusing on OOP, something which I am having a hard to master, please note after working with PHP for a number of years in procedural style the switch is not easy so please consider this is a beginner question.

So my textbook has the following example to create a class Database using PDO. enter image description here

Now I know I am probably going to come under attack from the PDO gang by saying the following but I prefer Mysqli for the time being as I don't have enough time to get comfortable with PDO at the moment. Thus I amtrying to convert the following class to Mysqli but by the looks of it failing horrible in the process.

I came up with the following code:

class Database{
    private static $server = 'localhost';
    private static  $uname = 'root';
    private static $pword = '';
    private static $dataBase = 'shop';

    private function __construct(){
    }

    public static function getDB(){
        if(!isset(self::$dataBase)){
            self::$dataBase = new mysqli_connect(
                self::$server,
                self::$uname,
                self::$pword
                                            );
        $db_found = mysqli_select_db(self::$dataBase);
        }//if
        if($db_found){
                echo 'SUCCESS'; 
            }//db found     
            return self::$dataBase;
        }
    }

Now unfortunately I don't have specific debugging errors to provide, except that I cant connect to my DB, since my console is not giving any errors, however I will point out a few areas which I believe is possibly incorrect.

  1. This is possibly incorrect self::$dataBase = new mysqli_connect
  2. Im not sure how to select my db inside the class thus, $db_found = mysqli_select_db(self::$dataBase); is possibly invalid...?

ANY help / advice very much appreciated!

Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97
  • 1
    You don't have time to get familiar with PDO, but apparently mysqli also isn't your forte…? Honestly, PDO's API is much simpler than mysqli's, just jump in, especially if your text book already builds on it. – deceze Oct 14 '16 at 07:54
  • 1
    @deceze you are right it isn't, `mysql_*` used to be, but since that is obsolete now I found `mysqli_*` procedural to be the closest to what I was familiar with, when I say I don't have time it simply means I have 6 other modules and as much as I would like to get to know PDO I first need to get through the exams – Timothy Coetzee Oct 14 '16 at 07:56
  • 1
    To be honest, I'm not convinced that chucking your database connector into a Singleton pattern in the first place is necessarily the best idea anyway... – CD001 Oct 14 '16 at 08:13
  • 2
    Your error though is that you're using `self::$dataBase` as both the **name** of the schema and the **connector** itself. `if(!isset(self::$dataBase))` is **never** going to trigger as `self::$dataBase` is always set `private static $dataBase = 'shop';` - you should have one private class member for the name of your schema and another for the connector. – CD001 Oct 14 '16 at 08:18
  • 1
    I am the only chap around to have a gold badge in [tag:mysqli], so you can tell that I've got a bit of experience with it. Consider this comparison, that I managed to compile, before making your final decision, [Usability problems of mysqli compared to PDO](https://phpdelusions.net/pdo/mysqli_comparison). In short, most likely your choice is based on the underestimating the importance of prepared statements. – Your Common Sense Oct 14 '16 at 08:18
  • @YourCommonSense gave it a quick scan looks like a very interesting read, will definitely give it some attention – Timothy Coetzee Oct 14 '16 at 08:24
  • As of the particular issue at hand, it's just a silly syntax error that will be revealed in an instant as soon as the proper error reporting in PHP is set, – Your Common Sense Oct 14 '16 at 08:25
  • @YourCommonSense WOW that is a bit harsh, can't see how it is a duplicate, let alone an exact duplicate, was hoping to get a bit more advice regarding specific question, but hey who am I to argue I am just a lowly ranked member. – Timothy Coetzee Oct 14 '16 at 08:34
  • You see, you can fed a man with a fish, or you can give him a fishing rod. I prefer the latter. Ego aside, what makes you so reluctant to setting the proper error reporting for PHP? – Your Common Sense Oct 14 '16 at 08:36
  • @YourCommonSense except that code will never actually reach the error in `mysql_select_db` calling `Database::getDB()` will skip the connector entirely and simply return the string `"shop"` – CD001 Oct 14 '16 at 08:36
  • @YourCommonSense I swear with my right hand that I know for a fact error reporting is on, and I fully understand your idiom, I was never asking for a solution just for advice on specific question, with all due respect I feel if you do want to close it you should mark it duplicate / or close it with something else not to the question you linked to – Timothy Coetzee Oct 14 '16 at 08:39
  • 1
    @CD001 sorry my bad. That's always the thing with "debug my code for me" questions - you spot one issue but miss all others – Your Common Sense Oct 14 '16 at 08:44
  • Timothy please pay attention the the earlier comment from @CD001 which it seems we both missed when I intervened. – Your Common Sense Oct 14 '16 at 08:49

0 Answers0