-4

Suppose I have an array as following

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

Number of items in the array is not fixed as they are selected from a 'select multiple lists'.

enter image description here

The list may consist more than 3 items and any number of items may be selected. Then submit button is pressed. How can I create a select statement where the column headings are taken from the array mentioned above. Thanks.

4
  • MySQL IN Clause tutorialspoint.com/mysql/mysql-in-clause.htm Commented Mar 29, 2017 at 6:59
  • What's your target? You really want to have a these array values being the column header in the mysql result set or what? Commented Mar 29, 2017 at 6:59
  • Possible duplicate of Select from mysql table WHERE field='$array'? Commented Mar 29, 2017 at 7:02
  • I want the array values as column header in the mysql result set @AlexOdenthal Commented Mar 29, 2017 at 7:03

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

$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);

Check this, https://www.w3schools.com/php/func_string_implode.asp

1 Comment

In this case, you have to make sure that string will pass like in IN clause ('str1','str2','str3')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.