简体   繁体   English

扩展mysqli_stmt以使用自定义的mysqli_result

[英]extending mysqli_stmt to use a custom mysqli_result

I've been looking for an answer to this for quite a while now, but it seems nobody ever got around this problem before. 我一直在寻找这个问题的答案,但似乎以前没有人能解决这个问题。 Maybe some of you are able and willing to help me out on this... that would be great! 也许你们中的一些人能够并愿意帮我解决这个问题......那太好了!

Im currently working on a mysqli wrapper and trying to implement a custom result class for prepared statements like i do for standard queries already! 我目前正在研究一个mysqli包装器并尝试为已准备好的语句实现自定义结果类,就像我已经为标准查询做的那样! It seems the result gets generated in the stmt's "execute" method but i still fail to understand what's going on behind the scenes! 似乎结果是在stmt的“执行”方法中生成的,但我仍然无法理解幕后发生了什么!

Is there a way (or hack) to point the generated results to my result class instead of the plain mysqli_result like its done with regular queries? 有没有办法(或黑客)将生成的结果指向我的结果类而不是普通的mysqli_result,就像使用常规查询一样?

Just to get you an idea, here's a little paste from the code : 只是为了给你一个想法,这里有一些来自代码的粘贴:

class extended_mysqli extends mysqli
{
    public function __construct()
    {
        call_user_func_array(array(get_parent_class($this), 'mysqli'), func_get_args());

        if ( $this->connect_errno )
        {
            throw new extended_mysqli_exception('database connection failure');
        }
    }

    public function query ($query, $binds = array())
    {
        if ( empty( $binds ) )
        {
            if ( $this->real_query($query) )
            {
                if ( $this->field_count )
                {
                    return new extended_mysqli_result($this, $query); // select, show, describe
                }
                else return true; // insert, update, delete
            }
            else return false; // fix 
        }
        else
        {
            $stmt = $this->prepare($query);

            if ( $stmt->bind_array($binds) )
            {
                return $stmt->execute() ? $stmt->get_result() : false;
            }
            else return false; 
        }
    }

    public function prepare($query)
    {
        return new extended_mysqli_stmt($this, $query);
    }

    // ...
}

class extended_mysqli_stmt extends mysqli_stmt
{
    public function __construct($link, $query)
    {
        parent::__construct($link);

        $this->prepare($query);
    }

    public function execute()
    {
        //  what do i do here ???
    }
}

class extended_mysqli_result extends mysqli_result implements countable, iterator, arrayaccess
{
    public function __construct($link, $mode = MYSQLI_STORE_RESULT)
    {
        parent::__construct($this->link = $link, $mode);
    }

    // ...
}

Those classes were ment to be used as it is, so if you want your own implementation for mysqli you should write your own wrapper using mysqli_* functions. 这些类是按原样使用的,所以如果你想要自己的mysqli实现,你应该使用mysqli_ *函数编写自己的包装器。

mysqli Objects can be extended but this approach will ultimately fail if you wish to do any change to its structure or hierarchy. mysqli对象可以扩展,但如果您希望对其结构或层次结构进行任何更改,此方法最终将失败。

Also, most of this methods trust your input data, so if you are trying to maintain a secure connection why not trying one of the 999 database abstraction layers out there and extending that code so it fits your needs? 此外,大多数此类方法都信任您的输入数据,因此,如果您尝试维护安全连接,为什么不尝试其中一个999数据库抽象层并扩展该代码以满足您的需求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM