2017-03-29 51 views
-4

Mysql的選擇語句假設我有一個數組,因爲它們是從一個「選擇多個列表」選擇爲以下使用PHP陣列

$sql_column_headings = array("Week Period", "Comments Approved", "Comment Replies"); 

數組中的項是不固定的數量。

enter image description here

該列表可以由多於3項,並且可以選擇任何數量的項目。然後按下提交按鈕。 如何創建一個select語句,其中的列標題是從上面提到的數組中獲取的。謝謝。

+0

MySQL的IN子句https://www.tutorialspoint.com/mysql/mysql-in-clause.htm –

+0

你叫什麼目標?你真的想要一個這些數組值作爲mysql結果集中的列標題或什麼? –

+0

[Select from mysql table WHERE field ='$ array'?](http://stackoverflow.com/questions/2382831/select-from-mysql-table-where-field-array) –

回答

1
$sql_column_headings = array("Week Period", "Comments Approved", "Comment Replies"); 

    $columns = implode(",",$sql_column_headings); 

    $sql = "SELECT '$columns' FROM table_name"; 

    $result = mysql_query($sql); 
1

試試這個,

$sql_column_headings = array("Week Period", "Comments Approved", "Comment Replies"); 

    $itemStr = implode(",",$sql_column_headings); 

    $qry = "SELECT * FROM table_name WHERE field_name IN('$itemStr')"; 

    $resSet = mysql_query($qry); 

檢查這一點, https://www.w3schools.com/php/func_string_implode.asp

+0

在這種情況下,您必須確保字符串像IN子句中那樣傳遞('str1','str2','str3') –