使用 PDO 连接时出现问题

发布于 2024-10-12 12:44:43 字数 1371 浏览 2 评论 0原文

这是我第一次使用 PDO 来进行测试。 但出现了一个奇怪的错误,谷歌了一下,似乎很奇怪。

这是我的数据库测试类

class db extends PDO
{
    # Our instance.
    private static $db = NULL;

    # Calling the connector.
    public static function connect()
    {
        if (self::$db === NULL)
        {
            $class = __CLASS__;
            self::$db = new $class();
        }
        return self::$db;
    }

    # Connector.
    public function __construct() 
    { 
        $dns = 'mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host');
        self::$db = new PDO($dns, reg::get('db-username'), reg::get('db-password'));
        reg::delete('db-password');
    }

    # Quick reporting
    public function reportError($array)
    {
        if ($this->db != NULL) { echo 'Myself getting horny'; } // Just for testing, i'm not getting horny because of a mysql database connection!
    }

}

然后执行以下代码:

$db = new db();
$row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1));
echo $row['value'];

它显示了以下错误:

Warning: PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: No error: PDO constructor was not called in myfile.php on line 39

将第 39 行视为

$row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1));

It's the first time i'm using PDO just for testing purpose.
But a strange error occurred and googling it, it seems to be weird.

Here's my database testing class

class db extends PDO
{
    # Our instance.
    private static $db = NULL;

    # Calling the connector.
    public static function connect()
    {
        if (self::$db === NULL)
        {
            $class = __CLASS__;
            self::$db = new $class();
        }
        return self::$db;
    }

    # Connector.
    public function __construct() 
    { 
        $dns = 'mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host');
        self::$db = new PDO($dns, reg::get('db-username'), reg::get('db-password'));
        reg::delete('db-password');
    }

    # Quick reporting
    public function reportError($array)
    {
        if ($this->db != NULL) { echo 'Myself getting horny'; } // Just for testing, i'm not getting horny because of a mysql database connection!
    }

}

Then executing the following code:

$db = new db();
$row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1));
echo $row['value'];

It shows me the following error:

Warning: PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: No error: PDO constructor was not called in myfile.php on line 39

Considering line 39 as

$row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沐歌 2024-10-19 12:44:43

你的代码一团糟,可能是因为你太饥渴了...

connect() 方法 - 为什么它在那里?:

if ($db === NULL)

应该是:

if (self::$db === NULL)

self::$db = new $class();

所以,如果 $class == __CLASS__ = = db,你正在做self::$db = new db();,似乎不对。


您不能使用 PDO 准备标识符,例如表或列。

$db->prepare('SELECT * FROM :table WHERE id = :id')->execute(array('table' => 'test', 'id' => 1));

应该是:

$db->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1));

试试这个:

class db extends PDO
{
    private static $db = null;

    public static function singleton()
    {
        if (is_null(self::$db) === true)
        {
            self::$db = new PDO('mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host'), reg::get('db-username'), reg::get('db-password'));
        }

        return self::$db;
    }
}

像这样:

$result = db::singleton()->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1));

var_dump($result);

You code is a mess, it's probably because you're so horny...

connect() method - why is it there?:

if ($db === NULL)

should be:

if (self::$db === NULL)

self::$db = new $class();

So, if $class == __CLASS__ == db, you are doing self::$db = new db();, doesn't seem right.


You can't use PDO to prepare an identifier, like a table or a column.

$db->prepare('SELECT * FROM :table WHERE id = :id')->execute(array('table' => 'test', 'id' => 1));

Should be:

$db->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1));

Try this:

class db extends PDO
{
    private static $db = null;

    public static function singleton()
    {
        if (is_null(self::$db) === true)
        {
            self::$db = new PDO('mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host'), reg::get('db-username'), reg::get('db-password'));
        }

        return self::$db;
    }
}

Like this:

$result = db::singleton()->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1));

var_dump($result);
云巢 2024-10-19 12:44:43

我不完全确定这是否是您的答案,因为它似乎与错误消息无关,但我认为您不能将表名作为绑定参数传递。如果将硬编码的表名称放在 :table 的位置会发生什么?

I'm not entirely sure if this is your answer, as it seems to be unrelated to the error message, but I do not think you can pass in a table name as a bound parameter. What happens if you put a hard-coded table name in the place of :table?

仅此而已 2024-10-19 12:44:43

您有一个静态“连接”构造函数,它在(静态)中创建一个数据库对象并返回它。但你也自己创建一个新的 db() 。

准备语句使用self::$db,因此尝试调用静态变量。我不太确定您的代码应该如何工作,将某种单例/静态形式与对象形式相结合。

但这似乎是麻烦所在

You have a static 'connect' constructor, that makes a db-object in (static), and returns it. But you also make a new db() yourself.

The prepare statement uses self::$db, so tries to call the static made variable. I'm not really sure how your code is supposed to work, combining some sort of singleton/static form with an object form.

But that seems to be the trouble

岁月无声 2024-10-19 12:44:43

您的单例模式完全错误。您的构造函数应该是 private 并且您应该使用 $db 的静态实例。请参阅此单例示例

Your singleton pattern is all wrong. Your constructor should be private and you should use static instances of $db. Refer to this singleton example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文