使用准备好的语句时出现“SQL 翻译错误:额外的占位符”

问题描述 投票:0回答:0

我试图在我的 SQL 中使用准备好的语句,但我无法让它工作。我收到错误“SQL 翻译错误:额外的占位符” 当我将这样的东西添加到我的代码

if ($cpucooler_socket != "") {

            $myArray = explode(',', trim($cpucooler_socket));

            for ($i = 0; $i < count($myArray); $i++) {

                if(count($myArray)>1) {

                    $socket = $myArray[$i];

                    if ($i === 0) {
                        $query .= " AND (socket LIKE ?";
                        $query_params[] = '%' . $socket . '%';
                    } else if ($i === count($myArray) - 1) {
                        $query .= " OR socket LIKE ?)";
                        $query_params[] = '%' . $socket . '%';
                    } else {
                        $query .= " OR socket LIKE ?";
                        $query_params[] = '%' . $socket . '%';
                    }

                } else {
                    $query .= " AND socket LIKE ?";
                    $query_params[] = '%' . $socket . '%';
                }
            }
        }

我做错了什么?在这种情况下我应该使用什么正确的语法?如果我不使用 for 循环,我只添加例如:

$query .= " AND socket LIKE ?";
$query_params[] = '%' . $myArray[0] . '%';

然后它工作正常。如果有帮助,这是我的完整代码:

public function getCompatibleMb($case_format_dosky, $cpu_socket, $ram_typ, $pocet_ram, $intel_socket, $amd_socket, $select_after_id, $search) {

    $cpucooler_socket = null;

    if(isset($intel_socket) || isset($amd_socket)){
        if ($intel_socket != null && $amd_socket != null) {
            $cpucooler_socket = $intel_socket.", ".$amd_socket;
        } else if($intel_socket != null) {
            $cpucooler_socket = $intel_socket;
        } else if ($amd_socket != null){
            $cpucooler_socket = $amd_socket;
        }
    } else {
        $cpucooler_socket = null;
    }

    $query_params = array();

    $query = "SELECT id encryptid, id,produkt,vyrobca,dostupnost,cena,socket,series,chipset,form_factor,bluetooth,wifi,rgb,m2,sata3,sietova_karta,zvukova_karta,pci_express_3_0,pci_express_4_0,pci_express_5_0,ram_type,ram_slots,rezim_ram,max_mhz_ram,mosfet_coolers,crossfire_support,sli_support,raid_support,audio_chipset,audio_channels,ext_connectors,int_connectors,max_lan_speed,pci_x16_slots,pci_x4_slots,pci_x1_slots,m2_ports,usb_2_0,usb_3_2_gen_1,usb_3_1_gen_2,usb_3_2_gen_2,sata_3_ports,img_count,produkt_number,vyrobca_url FROM mb_list WHERE dostupnost=1";

    if ($cpu_socket != "") {
        $query .= " AND socket = ?";
        $query_params[] = $cpu_socket;
    } else {
        if ($cpucooler_socket != "") {

            $myArray = explode(',', trim($cpucooler_socket));

            for ($i = 0; $i < count($myArray); $i++) {

                if(count($myArray)>1) {

                    $socket = $myArray[$i];

                    if ($i === 0) {
                        $query .= " AND (socket LIKE ?";
                        $query_params[] = '%' . $socket . '%';
                    } else if ($i === count($myArray) - 1) {
                        $query .= " OR socket LIKE ?)";
                        $query_params[] = '%' . $socket . '%';
                    } else {
                        $query .= " OR socket LIKE ?";
                        $query_params[] = '%' . $socket . '%';
                    }

                } else {
                    $query .= " AND socket LIKE ?";
                    $query_params[] = '%' . $socket . '%';
                }
            }
        }
    }

    if ($ram_typ != "") {
        $query .= " AND ram_type = ?";
        $query_params[] = $ram_typ;
    }

    if ($pocet_ram != "") {
        $query .= " AND ram_slots >= ?";
        $query_params[] = $pocet_ram;
    }

    if($case_format_dosky !="") {

        $myArray = explode(',', trim($case_format_dosky));

        for ($i = 0; $i < count($myArray); $i++) {

            if(count($myArray)>1) {

                $formfactor = trim($myArray[$i]);

                if ($i === 0) {
                    $query .= " AND (form_factor = ?";
                    $query_params[] = $formfactor;
                } else if ($i === count($myArray) - 1) {
                    $query .= " OR form_factor = ?";
                    $query_params[] = $formfactor;
                } else {
                    $query .= " OR form_factor = ?)";
                    $query_params[] = $formfactor;
                }

            } else {
                $query .= " AND form_factor LIKE ?";
                $query_params[] = '%' . $formfactor . '%';
            }
        }
    }

    if ($select_after_id != "") {
        $decrypted = $this->McryptServices->decryptData($select_after_id);
        $query .= " AND id > ?";
        $query_params[] = $decrypted;
    }
    if ($search != "") {
        $new_search = str_replace(" ", "%",$search);
        $query .= " AND produkt LIKE ?";
        $query_params[] = '%'.$new_search.'%';
    }

    $query .= " LIMIT 32";

    $vytah = $this->Database->query($query, $query_params)->fetchAll();

    $arr[] = $vytah;

    foreach ($arr[0] as $key => $value) {
        $arr[0][$key]->{'encryptid'} = $this->McryptServices->encryptData($arr[0][$key]->{'id'});
    }

    return json_encode($arr);

}

这是错误后的响应:

"line":225,"args":[["string","SELECT id encryptid, id,produkt,vyrobca,dostupnost,cena,socket,series,chipset,form_factor,bluetooth,wifi,rgb,m2,sata3,sietova_karta,zvukova_karta,pci_express_3_0,pci_express_4_0,pci_express_5_0,ram_type,ram_slots,rezim_ram,max_mhz_ram,mosfet_coolers,crossfire_support,sli_support,raid_support,audio_chipset,audio_channels,ext_connectors,int_connectors,max_lan_speed,pci_x16_slots,pci_x4_slots,pci_x1_slots,m2_ports,usb_2_0,usb_3_2_gen_1,usb_3_1_gen_2,usb_3_2_gen_2,sata_3_ports,img_count,produkt_number,vyrobca_url FROM mb_list WHERE dostupnost=1 AND (form_factor = ? OR form_factor = ? OR form_factor = ?) LIMIT 32"],["array",[["string","ATX"],["string","mATX (Micro ATX)"],["string","mITX (Mini ITX)"]]]]}
php mysql sql database prepared-statement
© www.soinside.com 2019 - 2024. All rights reserved.