0
votes

I'm trying to connect using a simle db class. For some reason it only print out "Initiate DB class"

test.php

include 'db.class.php';
echo 'Initiate DB class';
$db = new DB();
echo 'DB class did load';

db.class.php

class DB extends mysqli {
private static $instance = null;
private function __construct () {
    parent::init();
    $host = 'localhost'; 
    $user = 'root'; 
    $pass = 'MY_PASS';
    $dbse = 'MY_DB';
    parent::real_connect($host, $user, $pass, $dbse);
    if (0 !== $this->connect_errno):
        die('MySQL Error: '. mysqli_connect_error());
        //throw new Exception('MySQL Error: '. mysqli_connect_error());
    endif;
    }
    public function fetch ($sql, $id = null, $one = false) {
        $retval = array();
        if ($res = $this->query($sql)):
            $index = 0;
            while ($rs = $res->fetch_assoc()):
                if ($one):
                    $retval = $rs; break;
                else:
                    $retval[$id ? $rs[$id] : $index++] = $rs;
                endif;
            endwhile;
            $res->close();
        endif;
        return $retval;
    }

}

I have tried to search my log files for error but they come out empty.

3
Why just reinventing the wheel? Just use PDO object.B001ᛦ
Might consider that as well. But right now I'm just trying to find what I do wrong here.Andreas
Add this to the top of your script ini_set('display_errors', '1'); error_reporting(E_ALL); and run it again. That should (hopefully) print out any errors.Josh J

3 Answers

0
votes

Try this:

Db_class:

class Db_class{
/***********************CONNECT TO DB*********************/
public function db_connect(){
    $user       = '***';
    $db         = '***';
    $password   = '***';
    $host       = '***';

    try {
        $dbh = new PDO("mysql:host=$host;dbname=$db", $user, $password);
    }
    catch(PDOException $err) {
        echo "Error: ".$err->getMessage()."<br/>";
        die();
    }
    return $dbh;
}
/******************PREPARE AND EXECUTE SQL STATEMENTS*****/
public function query($statement){
    $keyword = substr(strtoupper($statement), 0, strpos($statement, " "));
    $dbh = $this->db_connect();     
    if($dbh){
        try{
            $sql = $dbh->prepare($statement);
            $exe = $sql->execute();
        }
        catch(PDOException $err){
            return $err->getMessage();
        }
        switch($keyword){
            case "SELECT": 
                $result = array();
                while($row = $sql->fetch(PDO::FETCH_ASSOC)){
                    $result[] = $row;
                }
                return $result;
                break;
            default: 
                return $exe;
                break;
        }
    }
    else{
        return false;
    }
}

Other PHP:

    $db = new Db_class();

    $sql = "SQL STATEMENT";
    $result = $db->query($sql);
0
votes

Ok got it,

In your call to db your calling new DB(); which mean you're trying to call the constructor of your DB class.

In your DB class it looks like you're trying to create a singleton, but something is missing normally there would be something to assign the instance the database connection, and something that asks the instance if it's empty create a new connection or if it's not use the same instance.

At the end of the day to make this work you can change your constructor to public.

0
votes

Your constructor is marked as private which means new DB will raise an error. I see you have a private property to store an instance, are you missing the singleton method to return a new object?