第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

多次使用綁定參數(shù)

多次使用綁定參數(shù)

湖上湖 2019-10-21 10:38:27
我正在嘗試為我的數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)非常基本的搜索引擎,其中用戶(hù)可能包含各種信息。搜索本身包含幾個(gè)聯(lián)合選擇,其中結(jié)果總是合并為3列。但是,返回的數(shù)據(jù)是從不同的表中獲取的。每個(gè)查詢(xún)都使用$ term進(jìn)行匹配,并將其綁定到“:term”作為預(yù)備參數(shù)?,F(xiàn)在,該手冊(cè)說(shuō):調(diào)用PDOStatement :: execute()時(shí),對(duì)于要傳遞給語(yǔ)句的每個(gè)值,必須包含一個(gè)唯一的參數(shù)標(biāo)記。您不能在準(zhǔn)備好的語(yǔ)句中兩次使用相同名稱(chēng)的命名參數(shù)標(biāo)記。我想,不是用:termX替換每個(gè):term參數(shù)(x表示term = n ++),必須有一個(gè)更好的解決方案?還是我只需要綁定X個(gè):termX?編輯為此發(fā)布我的解決方案:$query = "SELECT ... FROM table WHERE name LIKE :term OR number LIKE :term";$term = "hello world";$termX = 0;$query = preg_replace_callback("/\:term/", function ($matches) use (&$termX) { $termX++; return $matches[0] . ($termX - 1); }, $query);$pdo->prepare($query);for ($i = 0; $i < $termX; $i++)    $pdo->bindValue(":term$i", "%$term%", PDO::PARAM_STR);好了,這是一個(gè)示例。我沒(méi)有時(shí)間使用sqlfiddle,但如有必要,我會(huì)在后面添加一個(gè)。(    SELECT        t1.`name` AS resultText    FROM table1 AS t1    WHERE        t1.parent = :userID        AND        (            t1.`name` LIKE :term            OR            t1.`number` LIKE :term            AND            t1.`status` = :flagStatus        ))UNION(    SELECT        t2.`name` AS resultText    FROM table2 AS t2    WHERE        t2.parent = :userParentID        AND        (            t2.`name` LIKE :term            OR            t2.`ticket` LIKE :term            AND            t1.`state` = :flagTicket        ))
查看完整描述

3 回答

?
小怪獸愛(ài)吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

我已經(jīng)遇到過(guò)相同的問(wèn)題好幾次了,我想我已經(jīng)找到了一個(gè)非常簡(jiǎn)單和好的解決方案。萬(wàn)一我想多次使用參數(shù),我只是將它們存儲(chǔ)到MySQL中User-Defined Variable。

這使代碼更具可讀性,并且您在PHP中不需要任何其他功能:


$sql = "SET @term = :term";


try

{

    $stmt = $dbh->prepare($sql);

    $stmt->bindValue(":term", "%$term%", PDO::PARAM_STR);

    $stmt->execute();

}

catch(PDOException $e)

{

    // error handling

}



$sql = "SELECT ... FROM table WHERE name LIKE @term OR number LIKE @term";


try

{

    $stmt = $dbh->prepare($sql);

    $stmt->execute();

    $stmt->fetchAll();

}

catch(PDOException $e)

{

    //error handling

}

唯一的缺點(diǎn)可能是您需要執(zhí)行其他MySQL查詢(xún)-但恕我直言,這是完全值得的。

由于User-Defined Variables在MySQL中是會(huì)話(huà)綁定的,因此也不必?fù)?dān)心變量會(huì)@term在多用戶(hù)環(huán)境中產(chǎn)生副作用。


查看完整回答
反對(duì) 回復(fù) 2019-10-21
?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

我創(chuàng)建了兩個(gè)函數(shù)來(lái)通過(guò)重命名重復(fù)使用的術(shù)語(yǔ)來(lái)解決該問(wèn)題。一種用于重命名SQL,另一種用于重命名綁定。


    /**

     * Changes double bindings to seperate ones appended with numbers in bindings array

     * example: :term will become :term_1, :term_2, .. when used multiple times.

     *

     * @param string $pstrSql

     * @param array $paBindings

     * @return array

     */

    private function prepareParamtersForMultipleBindings($pstrSql, array $paBindings = array())

    {

        foreach($paBindings as $lstrBinding => $lmValue)

        {

            // $lnTermCount= substr_count($pstrSql, ':'.$lstrBinding);

            preg_match_all("/:".$lstrBinding."\b/", $pstrSql, $laMatches);


            $lnTermCount= (isset($laMatches[0])) ? count($laMatches[0]) : 0;


            if($lnTermCount > 1)

            {

                for($lnIndex = 1; $lnIndex <= $lnTermCount; $lnIndex++)

                {

                    $paBindings[$lstrBinding.'_'.$lnIndex] = $lmValue;

                }


                unset($paBindings[$lstrBinding]);

            }

        }


        return $paBindings;

    }


    /**

     * Changes double bindings to seperate ones appended with numbers in SQL string

     * example: :term will become :term_1, :term_2, .. when used multiple times.

     *

     * @param string $pstrSql

     * @param array $paBindings

     * @return string

     */

    private function prepareSqlForMultipleBindings($pstrSql, array $paBindings = array())

    {

        foreach($paBindings as $lstrBinding => $lmValue)

        {

            // $lnTermCount= substr_count($pstrSql, ':'.$lstrBinding);

            preg_match_all("/:".$lstrBinding."\b/", $pstrSql, $laMatches);


            $lnTermCount= (isset($laMatches[0])) ? count($laMatches[0]) : 0;


            if($lnTermCount > 1)

            {

                $lnCount= 0;

                $pstrSql= preg_replace_callback('(:'.$lstrBinding.'\b)', function($paMatches) use (&$lnCount) {

                    $lnCount++;

                    return sprintf("%s_%d", $paMatches[0], $lnCount);

                } , $pstrSql, $lnLimit = -1, $lnCount);

            }

        }


        return $pstrSql;

    }

用法示例:


$lstrSqlQuery= $this->prepareSqlForMultipleBindings($pstrSqlQuery, $paParameters);

$laParameters= $this->prepareParamtersForMultipleBindings($pstrSqlQuery, $paParameters);

$this->prepare($lstrSqlQuery)->execute($laParameters);

有關(guān)變量命名的說(shuō)明:

p:參數(shù),l:函數(shù)

str:字符串中的局部,n:數(shù)字,a:數(shù)組,m:混合


查看完整回答
反對(duì) 回復(fù) 2019-10-21
  • 3 回答
  • 0 關(guān)注
  • 637 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)