![](/img/trans.png)
[英]Pass array as an argumento from jQuery to a php handler using $.get and $.param()
[英]Pass values from php array as param in UNION query
我想將數組中的值作為單個參數傳遞給查詢,並按以下方式使用它們
Array
(
[0] => some
[1] => text
)
我想要這種功能
public function getData($some,$text)
{
$sql = " select * from table where field1 = '{$some}' OR field3 = {$some} ";
$union = " UNION ";
$sql = " select * from table where field2 = '{$text}' or field4 = {$text} ";
}
注意:數組的索引編號可能為N。
您可以使用以下內容:
public function getQuery($dataArray) {
$queryArray = array();
foreach($dataArray as $data) {
$query = "SELECT * from `table`";
$conditionArray = array();
foreach($data as $key => $value) {
$conditionArray[] = "`$key` = `$value`";
}
if (!empty ($conditionArray)) {
$query .= " WHERE ". implode(" OR ", $conditionArray);
}
$queryArray[] = $query;
}
return implode(" UNION ", $queryArray);
}
由於您的查詢是在同一列上進行的,因此請嘗試使用IN運算符跳過UNIONS:
select * from table where field1 IN (val1,val2....)
然后,您可以將數組作為參數傳遞
public function getData($data)
{
if(!empty($data)) {
return " select * from table where field1 = ".implode(",",$data);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.