dpruwm6206 2016-08-11 13:23
浏览 41

在null上调用成员函数prepare() - PDO

I have searched every single answer regarding this and none seem to fully answer what's going on. Currently I am getting the error in the title when trying to prepare a statement with pdo.

I have tried:

Using a constructor instead

Using non-static

Using $this when I had a constructor

Catching PDO exceptions, none seem to be thrown

Here is main.php

class db {
    public static $db;
    public static $db2;

    public static function start() {
        $host = "localhost";
        $dbname = "dbname";
        $usr = "user";
        $ps = "pass";

        $pdo = "mysql:host=$host;dbname=$dbname";
        $opt = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_PERSISTENT => true
        ];
        try {
        db::$db = new PDO($pdo, $usr, $ps, $opt);

        return db::$db;
        } catch(PDOException $e) {
            $e->getMessage();
        }
    }
}

And then to initialize the database, I call the start function in index.php with db::start();

Now, to the error.

Within main.php, in a whole different class and function, I try to call this connection statically.

$stmt0 = db::$db->prepare("SELECT * FROM users WHERE uname=:u");

But when done, I receive the error in the title, on this line.

Most would probably think, hmm, there must be an error with the connection then if it is calling it null. If that's the case, something is preventing my exception from being thrown because I see nothing in the error logs or on the page.

  • 写回答

2条回答 默认 最新

  • drnrxv9383 2016-08-11 13:30
    关注

    Your PDO is failing to connect, and since you aren't doing anything with that condition (you catch it but then silently drop it), it's going under the radar.

    Try removing the try..catch block. If an error is happening in connection, you need to know!

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)