简体   繁体   English

mysql查询问题,其中(IN)-'和“

[英]Issue with a mysql query where (IN) - ' and "

I have an issue with a sql query. 我有一个SQL查询问题。 The query works until I add the third line : AND procurements.activity1 IN("'.$arr.'")' . 该查询将一直有效,直到我添加第三行: AND procurements.activity1 IN("'.$arr.'")'为止。 I guess the ' and " are not properly set. 我猜'"没有正确设置。

IN $arr is an array.. IN $arr是一个数组。

Security checks are not an issue here, they are being dealt with. 安全检查在这里不是问题,正在处理中。

My question is all about the IN("'.$arr.'")' line, really. 我的问题全是关于IN("'.$arr.'")'行的。

Thanks so much in advance for your help. 在此先感谢您的帮助。

if (isset($_GET['continent'])) {$requete= 'countries.region = "'.$continent.'"';}
if (isset($_GET['pays'])) {$requete=''.$requete.' AND countries.code_iso="'.$pays.'"';}
if (isset($_GET['activity1'])) {$requete=''.$requete.' AND procurements.activity1 IN("'.$arr.'")';}
if (isset($_GET['type_org'])) {$requete=''.$requete.' AND organisations.type_org="'.$type_org.'"';}


$query = $mysqli->query('SELECT 
        countries.code_iso,
        countries.region,
        countries.fr,
        countries.en,
        countries.flag,
        procurements.id,
        procurements.ref_org,
        procurements.ref_bid,
        procurements.activity1,
        procurements.url,
        procurements.code_cpv,
        procurements.date_entered,
        procurements.date_expire,
        procurements.country_exec,
        organisations.ref_org,
        organisations.name_organisation,
        organisations.type_org,
        organisations.cp,
        organisations.city,
        organisations.country
        FROM countries, procurements, organisations
        WHERE countries.code_iso = procurements.country_exec
        AND organisations.ref_org = procurements.ref_org
        AND '.$requete.'');

First of all, if you want to append something you your variable instead of this: 首先,如果要附加变量,请不要使用此变量:

$requete=''.$requete.'

use $requete .= ' AND countries.code_iso="'.$pays.'"'; 使用$requete .= ' AND countries.code_iso="'.$pays.'"'; .. ..

About your IN("'.$arr.'")' , if $arr is an php array then you cannot assign it to your query like that, instead use use implode to combine your array into a string.. 关于您的IN("'.$arr.'")' ,如果$arr是一个php array则无法将其分配给query ,而是使用use implode将数组组合成字符串。

The initial problem appears to be that you are trying to just concat in an array. 最初的问题似乎是您试图仅连接到一个数组中。 However from your comment this is not the case. 但是从您的评论来看并非如此。

However you state you use:- 但是您声明您使用:-

$arr = implode(',', $_GET['activity1']);

But with your code of:- 但是使用以下代码:

if (isset($_GET['activity1'])) {$requete=''.$requete.' AND procurements.activity1 IN("'.$arr.'")';}

you would land up with something like:- 你会得到类似的东西:

AND procurements.activity1 IN("1,2,3")

Either change 要么改变

$arr = implode(',', $_GET['activity1']);

to

$arr = implode('","', $_GET['activity1']);

or change 或改变

if (isset($_GET['activity1'])) {$requete=''.$requete.' AND procurements.activity1 IN("'.$arr.'")';}

to

if (isset($_GET['activity1'])) {$requete=''.$requete.' AND procurements.activity1 IN('.$arr.')';}

you can use this code 您可以使用此代码

$requete = '';

if (isset($_GET['continent'])) {$requete = ' AND countries.region = "'.$continent.'"';}
if (isset($_GET['pays'])) {$requete .=' AND countries.code_iso="'.$pays.'"';}
if (isset($_GET['activity1'])) {$requete .=' AND procurements.activity1 IN("'.implode(',',$arr).'")';}
if (isset($_GET['type_org'])) {$requete .=' AND organisations.type_org="'.$type_org.'"';}

and in query remove "AND" before $requete variable 并在查询中删除$ requete变量前的“ AND”

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

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