download2014711 2015-10-08 13:50
浏览 18
已采纳

DB类不会加载

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条回答 默认 最新

  • doujiene2845 2015-10-08 13:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭