title: PHP script for PIVOT in SQL
body: <p>I have a View <strong>Emp_PRJ_Art</strong> in my SQL Server as below.</p>
  
  <pre><code>UsedArticleNo | EmpName | Project | EmpRol  
  PART0001    |    Tintin | PRJ1    |   PL  
  PART0002    |   Haddok  | PRJ2    |   TL  
  PART0003    |   Poppey  | PRJ3    |   GET  
  PART0004    |   Archie  | PRJ4    |   PM  
  PART0005    |   Tintin  | PRJ1    |   PL  
  PART0006    |   Archie  | PRJ4    |   PM  
  PART0007    |   Tintin  | PRJ3    |   PL  
  PART0008    |   Haddok  | PRJ3    |   TL  
  PART0009    |   Poppey  | PRJ1    |   GET  
  </code></pre>
  
  <p>I want to get a result like this. The numbers under PRJ columns are total number of Article used by the employee for that particular Project.</p>
  
  <pre><code>EmpName |PRJ1 | PRJ2 |  PRJ3 | PRJ4 |  EmpRol  
  Archie  | 0   |  0   |   0   |  2   |  PM  
  Haddok  | 0   |  1   |   1   |  0   |  TL  
  Poppey  | 1   |  0   |   1   |  0   |  GET  
  Tintin  | 2   |  0   |   1   |  0   |  PL  
  </code></pre>
  
  <p>I used the SQL query like this and getting the result as required.  </p>
  
  <pre><code>SELECT *   
  FROM ( SELECT EmpName, UsedArticleNo, Project  
          FROM Emp_PRJ_Art)  
  PIVOT(COUNT(UsedArticleNo) FOR (Project) IN ('PRJ1','PRJ2','PRJ3',’PRJ4’))  
  ORDER BY EmpName;
  </code></pre>
  
  <p>Now my problem is, I am not able to use PHP to get this query run and finally display the same table as webpage, as I am getting in SQL Developer Console. Below is the PHP code not working. </p>
  
  <pre><code>    &lt;?php
  
  $backcol="bgcolor=#9fffa1"; // Light Green
  $colgreen1="bgcolor=#5EE060"; // Light Green
  
  $maxitems=1000; // Maximum items found to display
  
  $s=" &lt;small&gt; ";
  
  $user='odbc';
  $pass='abcd';
  $sid ='server1.myComp.com';
  
  $adr = "http://server1/WebEditor";
  
  $conn = oci_connect($user, $pass, $sid);
  if (!$conn) {
      echo "Unable to connect: " .  var_dump(OCIError()  );
      die();
  }
  
  }
  
  $cmd="SELECT * 
  FROM ( SELECT EmpName, Used ArticleNo, Project  FROM Emp_PRJ_Art)
  PIVOT(COUNT(ArticleNo) FOR (Project) IN ('PRJ1','PRJ2','PRJ3','PRJ4'))
  ORDER BY Emp Name";
  $stid = oci_parse($conn,$cmd); oci_execute($stid);
  echo     "&lt;tr&gt;
              &lt;th $backcol&gt; $s Employee Name
              &lt;th $backcol&gt; $s Project-1
              &lt;th $backcol&gt; $s Project-2
              &lt;th $backcol&gt; $s Project-3
              &lt;th $backcol&gt; $s Project-4
              &lt;th $backcol&gt; $s EmpRol
               ";
  while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  
      echo "&lt;tr&gt;\n";
      echo "&lt;td&gt; $s " .$row['EmpName']
          ."&lt;td&gt; $s " .$row['PRJ1']
          ."&lt;td&gt; $s " .$row['PRJ2']
          ."&lt;td&gt; $s " .$row['PRJ3']
          ."&lt;td&gt; $s " .$row['PRJ4']
          ."&lt;td&gt; $s " .$row['EmpRol']
          ;
  } 
  echo "&lt;/table&gt;\n";
  echo "&lt;br&gt;&lt;font color='blue'&gt;This is a test&lt;/font&gt;";
  exit;
  ?&gt;
  </code></pre>
  
  <p>The EmpName has 700 entries and Project has 70 entries. UsedArticle can be more than 10k.</p>