簡體   English   中英

擴展mysqli_stmt以使用自定義的mysqli_result

[英]extending mysqli_stmt to use a custom mysqli_result

我一直在尋找這個問題的答案,但似乎以前沒有人能解決這個問題。 也許你們中的一些人能夠並願意幫我解決這個問題......那太好了!

我目前正在研究一個mysqli包裝器並嘗試為已准備好的語句實現自定義結果類,就像我已經為標准查詢做的那樣! 似乎結果是在stmt的“執行”方法中生成的,但我仍然無法理解幕后發生了什么!

有沒有辦法(或黑客)將生成的結果指向我的結果類而不是普通的mysqli_result,就像使用常規查詢一樣?

只是為了給你一個想法,這里有一些來自代碼的粘貼:

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);
    }

    // ...
}

這些類是按原樣使用的,所以如果你想要自己的mysqli實現,你應該使用mysqli_ *函數編寫自己的包裝器。

mysqli對象可以擴展,但如果您希望對其結構或層次結構進行任何更改,此方法最終將失敗。

此外,大多數此類方法都信任您的輸入數據,因此,如果您嘗試維護安全連接,為什么不嘗試其中一個999數據庫抽象層並擴展該代碼以滿足您的需求?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM