当数据库中存在记录时,尝试使用PDO包装器并将结果显示为"NULL" [英] Trying PDO wrapper and getting result as 'NULL' when there is a record present in the database

查看:65
本文介绍了当数据库中存在记录时,尝试使用PDO包装器并将结果显示为"NULL"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从 https://phpdelusions.net/pdo/pdo_wrapper 中尝试了PDO包装.

首先是PDO包装器

class MyPDO
{
    protected static $instance;
    protected $pdo;

    public function __construct() {
        $opt  = array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
            PDO::ATTR_EMULATE_PREPARES   => FALSE,
        );
        $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
        $this->pdo = new PDO($dsn, DB_USER, DB_PASS, $opt);

    }

    // a classical static method to make it universally available
    public static function instance()
    {
        if (self::$instance === null)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }

    // a proxy to native PDO methods
    public function __call($method, $args)
    {
        return call_user_func_array(array($this->pdo, $method), $args);
    }

    // a helper function to run prepared statements smoothly
    public function run($sql, $args = [])
    {
        if (!$args)
        {
            return $this->query($sql);
        }
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($args);
        return $stmt;
    }
}

然后是用户类别

class User
{
    /* @var MyPDO */
    protected $db;

    protected $data;

    public function __construct()
    {
        $this->db = MyPDO::instance();
    }

    public function find($id)
    {
        $this->data = $this->db->run("SELECT * FROM user_account WHERE id = ?", [$id])->fetch();

    }
}

然后实例化类用户

$user = new User();
$a = $user->find(3);

var_dump($a);

有一条与ID = 3关联的记录,但是我得到的结果为NULL,为什么?

解决方案

如注释中所述,find方法不返回任何内容,它可能只是将其存储在$this->data中,但从未返回.

public function find($id)
{
    $this->data = $this->db->run("SELECT * FROM user_account WHERE id = ?", [$id])->fetch();
    return $this->data;
}

I just tried a PDO wrapper from https://phpdelusions.net/pdo/pdo_wrapper.

First the PDO Wrapper

class MyPDO
{
    protected static $instance;
    protected $pdo;

    public function __construct() {
        $opt  = array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
            PDO::ATTR_EMULATE_PREPARES   => FALSE,
        );
        $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
        $this->pdo = new PDO($dsn, DB_USER, DB_PASS, $opt);

    }

    // a classical static method to make it universally available
    public static function instance()
    {
        if (self::$instance === null)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }

    // a proxy to native PDO methods
    public function __call($method, $args)
    {
        return call_user_func_array(array($this->pdo, $method), $args);
    }

    // a helper function to run prepared statements smoothly
    public function run($sql, $args = [])
    {
        if (!$args)
        {
            return $this->query($sql);
        }
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($args);
        return $stmt;
    }
}

and then the user class

class User
{
    /* @var MyPDO */
    protected $db;

    protected $data;

    public function __construct()
    {
        $this->db = MyPDO::instance();
    }

    public function find($id)
    {
        $this->data = $this->db->run("SELECT * FROM user_account WHERE id = ?", [$id])->fetch();

    }
}

and then instantiate class user

$user = new User();
$a = $user->find(3);

var_dump($a);

There is a record associated with ID = 3 but the result I get is NULL, why?

解决方案

As mentioned in the comments, the find method doesn't return anything, it is probably storing it just fine in $this->data but is never returned.

public function find($id)
{
    $this->data = $this->db->run("SELECT * FROM user_account WHERE id = ?", [$id])->fetch();
    return $this->data;
}

这篇关于当数据库中存在记录时,尝试使用PDO包装器并将结果显示为"NULL"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆