title: How to Convert MySQL Column Value From Array to String
body: <p>I am working on an existing HTML form used to collect data about a project and then inserts that project record into a MySQL database using PHP.</p>
  
  <p>Inside the form, there is an input field named "staff[]". This field is a multi select element, that allows users to select more than one team member to handle the project.</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
      &lt;select multiple name="staff[]"&gt;
          &lt;option value="1"&gt;Mary&lt;/option&gt;
          &lt;option value="2"&gt;Tyrone&lt;/option&gt;
          &lt;option value="3"&gt;Rod&lt;/option&gt;
          &lt;option value="4"&gt;Marcus&lt;/option&gt;
          &lt;option value="5"&gt;David&lt;/option&gt;
      &lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>For example purposes, the user selects Tyrone, Rod and David for this particular project. If we insert the record at this point, the database only stores the first record value, which would be Tyrone's ID of 2. General practice is to store each instance in a separate table, however this is not our system and due to a restriction of 4 members for each project, management would prefer we insert a comma delimited array into each project's staff column for convenience.</p>
  
  <p>In order to handle this issue, we've created a foreach loop that loops through the selected values from the dropdown menu, while ensuring a trailing comma doesn't exist:</p>
  
  <pre><code>// Add array into one variable
  $staff_count = count($_POST['project_staff']);
  $i = 0;
  foreach($_POST['project_staff'] as $staff) {
      if (++$i === $staff_count) {
          $member_variable .= $staff;
      } else {
          $member_variable .= $staff . ", ";
      }
  }
  </code></pre>
  
  <p>After pressing the submit button, the above script is ran (which produces an array value of (2, 3, 5)) and the record is inserted into the 'projects' table with no issues.</p>
  
  <p>HEREIN LIES THE PROBLEM.</p>
  
  <p>Finally we have a view page, where we will call all employees assigned to a project, based on the query parameter, which would be the project ID. For example, if the previous project ID was 6, the following URL would be used:</p>
  
  <pre><code>site.com/project/view/?project=6
  </code></pre>
  
  <p>From this page, I am able to save the staff list using the following variable assignment:</p>
  
  <pre><code>$project = "SELECT * FROM projects WHERE project = 6";
  $employee_chosen = $project['project_employee']
  </code></pre>
  
  <p>If the 'staff' column only accepted one employee (for example, just one value of 4), the variable would have a value of one number:</p>
  
  <pre><code>$project['project_employee'] (4)
  </code></pre>
  
  <p>I would then be able to run a secondary query for employees as such:</p>
  
  <pre><code>$employee_chosen = $project['project_employee']; (4)
  query2 = "SELECT * FROM employees WHERE employee_ID = $employee_chosen";
  </code></pre>
  
  <p>This would very easily bring back the one employee that was entered in the "staff" column. However, we are dealing with an array in this column value (2, 3, 5) and so I have queried the following statement:</p>
  
  <pre><code>$employee_list = $project['project_staff']; (2,3,5)
  $query_employees = "SELECT * FROM employees WHERE employee_id IN ($employee_list)";
  </code></pre>
  
  <p>When I run this query, I receive only the first result from the employee ID 2 (as initially stated with the HTML form).</p>
  
  <p>However, if I use phpMyAdmin to directly type in the three numbers as a string:</p>
  
  <pre><code>$query_employees = "SELECT * FROM employees WHERE employee_id IN (2,3,5)";
  </code></pre>
  
  <p>I receive all three employee records.</p>
  
  <p>Just to ensure that the column ARRAY was in fact behaving as a STRING, I initiated a var_dump on the value:</p>
  
  <pre><code>echo var_dump($project['project_staff']);
  </code></pre>
  
  <p>After which I received the following information:</p>
  
  <pre><code>string(7) "4, 5, 6"
  </code></pre>
  
  <p>Does anyone have any ideas?</p>
  
  <p>I am satisfied with the idea that I am able to query the value, as before I received several non-object and array errors.</p>
  
  <p>Thanks in advance for any assistance you may be able to provide.</p>