2017-03-29 49 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') –